Difference between revisions of "Editing data into a model in Django"
(Created page with "This will assume you have created a model for 'Products' from the previous tutorial. Also the page to Add data into a model will create the Product Form. =views.py= Now withi...") |
(→views.py) |
||
Line 15: | Line 15: | ||
def editproduct(request, prodID): | def editproduct(request, prodID): | ||
if request.method=='POST': # this means the form has data | if request.method=='POST': # this means the form has data | ||
− | form = ProductForm(request.POST,instance=Product. | + | form = ProductForm(request.POST,instance=Product.objects.get(pk=prodID)) # get the form and it data |
if form.is_valid(): # check if it is valid | if form.is_valid(): # check if it is valid | ||
name = form.cleaned_data.get('name') # clean the data | name = form.cleaned_data.get('name') # clean the data | ||
Line 24: | Line 24: | ||
return redirect('\products') | return redirect('\products') | ||
else: # form not valid so display message and retain the data entered | else: # form not valid so display message and retain the data entered | ||
− | form = ProductForm(request.POST,instance=Product. | + | form = ProductForm(request.POST,instance=Product.objects.get(pk=prodID)) |
messages.success(request, 'Error in editing your product, the form is not valid!') | messages.success(request, 'Error in editing your product, the form is not valid!') | ||
return render(request, '/editproduct.html', {'form':form}) | return render(request, '/editproduct.html', {'form':form}) | ||
else: #the form has no data | else: #the form has no data | ||
− | form = ProductForm(instance=Product. | + | form = ProductForm(instance=Product.objects.get(pk=prodID)) #produce a blank form |
return render(request, '/editproduct.html', {'form':form}) | return render(request, '/editproduct.html', {'form':form}) | ||
</syntaxhighlight> | </syntaxhighlight> | ||
− | |||
=urls.py= | =urls.py= |
Latest revision as of 10:15, 15 March 2021
This will assume you have created a model for 'Products' from the previous tutorial. Also the page to Add data into a model will create the Product Form.
Contents
views.py
Now within your app folder we need to use 'views.py' to create a view for our add product form. We need to import your form and a few other functions, so make sure the following are imported:
from .forms import ProductForm
from django.contrib import messages
from django.shortcuts import redirect
If you already have a view to add a product, copy it and each time 'ProductForm' is used we need to pass the instance in the '()' brackets. 'prodID' is also used to get the product ID from the url:
def editproduct(request, prodID):
if request.method=='POST': # this means the form has data
form = ProductForm(request.POST,instance=Product.objects.get(pk=prodID)) # get the form and it data
if form.is_valid(): # check if it is valid
name = form.cleaned_data.get('name') # clean the data
manufacturer = form.cleaned_data.get('manufacturer') # clean the data
description = form.cleaned_data.get('description') # clean the data
form.save() # save the data to the model
messages.success(request, 'Your product has been edited!')
return redirect('\products')
else: # form not valid so display message and retain the data entered
form = ProductForm(request.POST,instance=Product.objects.get(pk=prodID))
messages.success(request, 'Error in editing your product, the form is not valid!')
return render(request, '/editproduct.html', {'form':form})
else: #the form has no data
form = ProductForm(instance=Product.objects.get(pk=prodID)) #produce a blank form
return render(request, '/editproduct.html', {'form':form})
urls.py
Now in the 'urls.py' file in the project folder we need to add a new urlpattern. So add the following:
path('editproduct/<int:prodID>/', myapp_views.editproduct, name='editproduct'),
editproduct.html
In the 'templates' folder in the 'MyApp' app, create the following 'html' page:
{% extends "MyApp/base.html" %}
{% block content %}
<h2>Edit Product</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit<button>
</form>
{% endblock %}
Testing
Run your server and visit your webapp, add '/editproduct/1' to the address bar to access your new edit product '1' page.