DRY::(Don’t Repeat Yourself)
Django provides with a lot of high-efficient tools which can speed up your developing process. Learning to use them is a big deal, however, you are going to learn much more than the syntax itself.
In a form, use {% csrf_token %}
is an easy and fast way to be shut cross-domin requests down.
like
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>form</title>
</head>
<body>
<p>{% if error_message %}{{ error_message }}</p>{% endif %}
<h1>form</h1>
<form action="{% url 'testx:form_action_test'%}" method="post">
{% csrf_token %}
{{ student.student_id }}
stuid
<input type="text" name="stuid"><BR>
stuname
<input type="text" name="stuname">
<input type="submit" value="GO">
</form>
</body>
</html>
To use the “student” mentioned in the code, you need to create a model for you database.
for example, if you modify your models.py into this
from django.db import models
# Create your models here.
class Student(models.Model):
student_id = models.CharField(max_length=30, default='0')
student_name = models.CharField(max_length=20, default='no_name')
when processing a object of Student in views.py, you can just create an object of Student and then change the values of its member and then use Student’s base method save to add your new object to DB.
stu = Student()
params = request.POST
stuid = params['stuid']
stuname = params['stuname']
stu.student_id = stuid
stu.student_name = stuname
stu.save()
After you’ve got the data from the form and added it to your database, you may want to show a result page to your visitor and the method you just used is a fully back-end method you may want your visitor to redirect to that result page.
use django.http.HttpResponseRedirect and django.urls.reverse to do the job.
like return HttpResponseRedirect(reverse('form:result', args=(pk,)))
this args is the url argument normally transferred by browsers.
when it comes to the front-end method, it’s even faster to be built by Django’s generic class.
from django.views import generic
class ResultView(generic.DetailView):
model = Student
template_name = 'form/formResult.html'
class IndexView(generic.ListView):
template_name = '/form/index.html'
context_object_name = 'all_students'
def get_queryset(self):
return Student.objects.all()
Use DetailView when you want to see some detail, because this basic class requires the name of you model and of course the template’s name.
It can automatically get the object from url argument where you must only use some parameters like <int:pk>
and such, which will be named as “student”, the lower case of Student.
In a ListView class you don’t have to specify your model’s name, rather you should specify the list’s name in HTML and how you get the list, the method.
Their url.py should look like this.
from django.urls import path
from . import views
from . import apps
app_name = apps.TestxConfig.name # my app name is 'testx'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('result/<int:pk>', views.ResultView.as_view(), name='form_result'),
]