Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

1.views.py

from django.shortcuts import render
from . models import User
from .forms import Userform

def Home(request):
    if request.method == "POST":
        form = Userform(request.POST or None,request.FILES or None)
        if form.is_valid():
            form.save()
    else:
        form = Userform()
    return render(request,"home.html",{"form":form})

def read(request):
    read = User.objects.all()
    return render(request,"read.html",{"read":read})

2.models.py

from django.db import models

class User(models.Model):
    name = models.CharField(max_length=12)
    rollno = models.IntegerField()
    # files = models.FileField()

3.form.py

from django import forms
from .models import User

class Userform(forms.ModelForm):
    class Meta:
        model =  User
        fields = ('name','rollno')
    

urls.py

from django.contrib import admin
from django.urls import path
from app import views
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = [
    path('admin/', admin.site.urls),
    path("",views.Home,name="Home"),
    path("read/",views.read, name="read"),
]

urlpatterns+=static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

4.home.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <form method="POST" action="read/" enctype="multipart/form-data">
        {%csrf_token%}
        {{form}}
    <button type=submit>click to submit</button>
    </form>
</body>
</html>

read.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    {%block containt%}
    {%for i in read%}
    {{i.name}}
    {{i.rollno}}
  
    {%endfor%}
    {%endblock%}
</body>
</html>

i want to add data to django admin using a form but data is not uploading at django admin Not able to upload data into django admin hence cant read it please help i want to add data to django admin using a form but data is not uploading at django admin Not able to upload data into django admin hence cant read it please help


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
128 views
Welcome To Ask or Share your Answers For Others

1 Answer

Problem is in the action of your form, you have entered the wrong URL in the action You have to put home URL instead of read/, because you are handling your form submission in your home function so replace your form action by this

<form method="POST" action="{% url 'Home' %}" enctype="multipart/form-data">
        {%csrf_token%}
        {{form}}
    <button type=submit>click to submit</button>
    </form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...