Difference between revisions of "Add data into a model in Django"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "This will assume you have created a model for 'Products' from the previous tutorial. =forms.py= Within your app folder you should have a file called 'forms.py', if you haven'...")
 
(forms.py)
Line 22: Line 22:
 
         model=Product
 
         model=Product
 
         fields = ['name','manufacturer','description']
 
         fields = ['name','manufacturer','description']
from .models import Product
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 28: Line 27:
  
 
The internal class 'Meta' binds the model to the form, and you can also specify which fields to show.
 
The internal class 'Meta' binds the model to the form, and you can also specify which fields to show.
 +
 +
=views.py=
 +
Now within your app folder we need to use 'views.py' to create a view for our add product form.
 +
 +
<syntaxhighlight lang=python>
 +
def addproduct(request):
 +
    if request.method=='POST':
 +
        form = ProductForm(request.POST)
 +
            if form.is_valid():
 +
                name = form.cleaned_data.get('name')
 +
                manufacturer = form.cleaned_data.get('manufacturer')
 +
                description = form.cleaned_data.get('description')
 +
                form.save()
 +
                messages.success(request, 'Your product has been added!')
 +
                return redirect('\')
 +
            else:
 +
                form = ProductForm(request.POST)
 +
                messages.success(request, 'Error in creating your product, the form is not valid!')
 +
                return render(request, '/addproduct.html', {'form':form})
 +
    else:
 +
        form = ProductForm()
 +
        return render(request, '/addproduct.html', {'form':form})
 +
</syntaxhighlight>

Revision as of 13:03, 17 September 2019

This will assume you have created a model for 'Products' from the previous tutorial.

forms.py

Within your app folder you should have a file called 'forms.py', if you haven't you need to create it.

You will need to import the following:

from django import forms
from .models import Product

You can then create a new class for your product form:

class ProductForm(forms.ModelForm):
    name = forms.CharField(required=True)
    manufacturer = forms.CharField(required=True)
    description = forms.CharField(required=True)

    class Meta:
        model=Product
        fields = ['name','manufacturer','description']

The 'ProductForm' class specifies data types and that some fields are required. The 'date_added' was set up to be automatic so no input is required.

The internal class 'Meta' binds the model to the form, and you can also specify which fields to show.

views.py

Now within your app folder we need to use 'views.py' to create a view for our add product form.

def addproduct(request):
    if request.method=='POST':
        form = ProductForm(request.POST)
            if form.is_valid():
                name = form.cleaned_data.get('name')
                manufacturer = form.cleaned_data.get('manufacturer')
                description = form.cleaned_data.get('description')
                form.save()
                messages.success(request, 'Your product has been added!')
                return redirect('\')
            else:
                form = ProductForm(request.POST)
                messages.success(request, 'Error in creating your product, the form is not valid!')
                return render(request, '/addproduct.html', {'form':form})
    else:
        form = ProductForm()
        return render(request, '/addproduct.html', {'form':form})