Home > OS >  Record not getting edited in django form using instance
Record not getting edited in django form using instance

Time:11-17

The model is not getting updated in the database while using the below methods.

This is upload form in views

def upload(request):
if request.method == 'POST':
    form = UploadForm(request.POST, request.FILES)
    if form.is_valid():
        upload = form.save(commit= False)
        upload.user = request.user
        upload.save()
        messages.info(request,"Added Successfully..!")
    return redirect("home")
return render(request, "upload.html", {'form': UploadForm})

This is my edit function in views

def editbug(request, pk):
edit = bug.objects.get(id=pk)

if request.method == 'POST':
    form = UploadForm(request.POST, instance= edit)
    if form.is_valid():
        form.save()
        print("uploaded")
    messages.info('Record updated successfully..!')
    return redirect("home")
else:
    form = UploadForm(instance= edit)


return render(request, "upload.html", {'form': form})

urls.py

urlpatterns = [
    path('home',views.home, name='home'),
    path('index',views.index, name='index'),
    path("records/<int:pk>/", views.records, name="records"),
    path("", views.login_request, name="login"),
    path("logout", views.logout_request, name="logout"),
    path("upload", views.upload, name='upload'),
    path("edit/<int:pk>/", views.editbug, name="edit")
]

Relevant Template:

 {% for bug in b %}
 <tr>
 <td>{{ forloop.counter }}</td>
 <td><a href="{% url 'records' pk=bug.pk %}">{{bug.name}}</a> 
</td>
<td>{{bug.created_at}}</td>
<td>{{bug.user}}</td>
<td>{{bug.status}}</td>
<td><a  href="{% url 'edit' bug.id 
 %}">Edit</a></td>
</tr>
{% endfor %}

This is the template used for editing the form

<!DOCTYPE html>
<html>
<head>
<style>
table {
 font-family: arial, sans-serif;
 border-collapse: collapse;
 width: 60%;
 margin-left: auto;
 margin-right: auto;
 }
td, th {
border: 1px solid #dddddd;
 text-align: left;
  padding: 8px;
 }

tr:nth-child(even) {
background-color: #dddddd;
}

</style>
</head>
 <body>

<h2 style="text-align: center;">Bug List</h2>

<table>
<tr>
<th style="width: 5%;">Sl no.</th>
<th>Bug</th>
<th style="width: 20%;">Created at</th>
<th>Created by</th>
<th>Status</th>
</tr>
{% block content %}
{% for bug in b %}
<tr>
<td>{{ forloop.counter }}</td>
<td><a href="{% url 'records' pk=bug.pk %}">{{bug.name}}</a> 
</td>
<td>{{bug.created_at}}</td>
<td>{{bug.user}}</td>
<td>{{bug.status}}</td>
<td><a  href="{% url 'edit' bug.id 
%}">Edit</a></td>
</tr>
  {% endfor %}
 {% endblock %}
 </table>

</body>
</html>

This is the upload form for the form creation and editing.

class UploadForm(ModelForm):
name = forms.CharField(max_length=200)
info = forms.TextInput()
status = forms.ChoiceField(choices = status_choice, widget= 
forms.RadioSelect())
fixed_by = forms.CharField(max_length=30)
phn_number = PhoneNumberField()
#created_by = forms.CharField(max_length=30)
#created_at = forms.DateTimeField()
#updated_at = forms.DateTimeField()
screeenshot = forms.ImageField()

class Meta:
    model = bug
    fields = ['name', 'info', 'status', 'fixed_by', 
    'phn_number', 'screeenshot']

Tried editing the record but it is not getting updated. please check the views, templates and urls.

CodePudding user response:

I have following suggestions:

  1. Only redirect if the form is valid.

  2. Also use request.FILES while editing.

  3. Use get_object_or_404() instead of get().

So, upload view should be:

def upload(request):
    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES)
        if form.is_valid():
            upload = form.save(commit= False)
            upload.user = request.user
            upload.save()
            messages.info(request,"Added Successfully..!")
            return redirect("home") #Only redirect if the form is valid.
    else:
        form=UploadForm()
    return render(request, "upload.html", {'form': form})

Edit view should be:

def editbug(request, pk):
    edit = get_object_or_404(bug, id=pk)

    if request.method == 'POST':
        form = UploadForm(request.POST, request.FILES, instance= edit)
        if form.is_valid():
            form.save()
            print("uploaded")
            messages.info('Record updated successfully..!')
            return redirect("home")
    else:
        form = UploadForm(instance=edit)

    return render(request, "edit.html", {'form': form})

Note: Models are written in PascalCase so it is better to name it as Bug instead of bug.

Make separate template for editing, say edit.html so:

<body>
    <form method='POST'>
        {% crsf_token %}
        {{form.as_p}}
        <input type='submit'>
    </form>
</body>

CodePudding user response:

Also you can try this way:

views.py:

def update(request, id):  
    edit = bug.objects.get(id=id)  
    form = Formname(request.POST,instance=edit)  
    if form.is_valid():  
       form.save()  
       return HttpResponseRedirect('/')  
    return render(request,'edit.html',{'edit': edit})

And in templates:

edit.html:

<form method='post' action=/update/{{edit.id}}>
   {% csrf_token %}
  <input value={{edit.fieldname}}/>
  <input type="submit"/>
</form>

urls.py:

path('update/<int:id>/',views.update)
  • Related