Create a forum application using django #2: Template system and Class-based view implementation
Repository
What Will I Learn?
- Template system
- Class-based view implementation
Requirements
- Basic Python
- Install Python 3
- Install Django
Resources
- Python - https://www.python.org/
- Django- https://www.djangoproject.com/
Difficulty
Basic
Tutorial Content
This tutorial is part of the previous tutorial series, this tutorial will create a forum application by using the Django framework, of course, in the making it takes several stages and some features that will be implemented. in the tutorial previously, we have discussed project initialization and basic structure in making this project. this tutorial will focus on creating an authentication feature in the application created.
Template system
We will continue the authentication section that was made in the previous tutorial. We have created routing the previous tutorial. now we will create a template system that uses the class-based view method. we will see how it is implemented in this project.
- Class based view method
There is one templating system on Django that is commonly used is class-based view. This method is most often used in Django because it is more structured and easier to maintain. We will create a template on the module account so we will configure the account/views.py. as a reminder in the previous tutorial section we have made a routing to be used for the user register, the following is the routing that is in the accounts/urls.py file.
accounts/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('signup/', views.SignUp.as_view(), name='signup')
//routing that we have created and connected with view signup
]
- Template configuration
now we will configure it so that we can use the template on urls.py. Here's how to configure it in the module account:
account/views.py
from django.shortcuts import render
from django.contrib.auth.forms import UserCreationForm //import form
from django.urls import reverse_lazy // import for rendering in URL
from django.views import generic // import module generic views
class SignUp(generic.CreateView):
from_class = UserCreationForm
success_url = reverse_lazy('login')
template_name = 'signup.html'
- Generic view
We will get to know new tools in Django. We will use the generic view function, what is generic view ?. Generic view is a view that has been built-in on Django. The generic view provides standard forms that are used for login registers and other forms. to use it we can import a view from a generic view like the following:
from django.contrib.auth.forms import UserCreationForm
: Import login form and registration automatically come from a generic viewfrom django.urls import reverse_lazy
: import function that will render the page in the URLfrom django.views import generic
: Import generic view so that we can use the auth view which is alias UserCreationForm.
Generic view class
Please note that we can create classes for each template so that we are easy to use. to make the class we have to inject the generic view as a parameter to the class that will be used as the following example:
class SignUp(generic.CreateView):
: Create classes to separate templates and to be used repeatedly. In this function, I define the SignUp class that we have defined in our login URL. we can see in the picture below:
from_class = UserCreationForm
: Django provides templates that have been automatically created by Django. so we can use it as needed. In this application, we will use the form in the generate view function.success_url = reverse_lazy('login')
: The reverse_lazy function is used to direct our template to the URL that have defined in this function. In this function, I will direct the direct to/login
.template_name = 'signup.html'
: Now we can determine which template will be rendered in this way template_name = 'signup.html', in this application the template will be rendered signup.html.
This is one of the advantages of using a class-based view, by using this method we do not need to set the configuration because we can use the CreateView function that Django has provided.
- Template rendering
Now the main focus is on how to render templates based on routing that is accessed. For that, we need to need to make a templates folder according to what is defined in settings.py.
Then to make the template can be in the templates folder, as defined above, the template that will be rendered is signup.html. The following is the content.
signup.html
<h2>Sign Up</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit" name="button">Register</button>
</form>
As mentioned in the previous section, on this page we will use the generic view that we injected in the SignUp class. to use the form you can use the following method
{{form.as_p}}
. Because what is created is our signup process will wrap it in the<form>
tag.And then adding the gar register button the user can register
<button type="submit" name="button">Register</button>
and we need to use{% csrf_token %}
for security on the form we have created
After all is done, we can run the server in Django like this python manage.py runserver
After running the server we will see how the view we have made can be rendered in the routing we are going to, as in the demonstration below:
Can be seen in the picture above we have succeeded in creating a templating system which is the result of the implementation of generic view, for the next tutorial I will discuss user authentication when registering, logging in and logging out. thank you for following this tutorial. hopefully, it will be useful for you
Curriculum
Tutorial Django - Class based view #2 : Use Class based view method and Get and Post method
Thank you for your contribution @duski.harahap.
After analyzing your tutorial we suggest the following points below:
Your tutorial is quite short for a good tutorial. We recommend you aim for capturing at least 2-3 concepts.
In your contribution try to put more features.
Looking forward to your upcoming tutorials.
Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.
To view those questions and the relevant answers related to your post, click here.
Need help? Chat with us on Discord.
[utopian-moderator]
Thank you for your review, @portugalcoin! Keep up the good work!
Hi @duski.harahap!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server
Hi @duski.harahap!
Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server
Hey, @duski.harahap!
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!
Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).
Want to chat? Join us on Discord https://discord.gg/h52nFrV.
Vote for Utopian Witness!