Python Django Part 3 - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript Python Django Part 3 - Supercoders | Web Development and Design | Tutorial for Java, PHP, HTML, Javascript

Breaking

Post Top Ad

Post Top Ad

Saturday, December 22, 2018

Python Django Part 3

Python Django Part 3




Users and forms

Most web applications need to let users create accounts. This lets users create and work with their own data. Some of this data may be private, and some may be public. Django's forms allow users to enter and modify their data.

User accounts

user accounts are handled by a dedicated app called users.User need to be able to register, log in, and log out. Django automates much of this work for you.


#Making a users app
#After making the app be sure to add users to INSTALLED_APPS in the project's setting.py file.

#$ python manage.py startapp users

#Including URLS for the users app
#Add a line to the project's urls.py file so the users app's URLS are included in the project.

urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^users/', include('users.urls', namespace='users')),
url(r'', include('learning_logs.urls', namespace='learning_logs')),
]

Using forms in DJango

There are a number of ways to create forms and work with them. You can use Django's defaults, or completely customize your forms. For a simply way to let users enter data based on models, use a ModelForm. This creates a form that allows users to enter data that will populate the fields on a model.
The register views on the back of this sheet shows a simple appraoch to forms processing. If the views doesn't receive data from a form, it responds with a blank form. If it receives POST data from a form, it validates the data and then saves it to the database

User accounts(cont.)

Defining the URLs 
#users will need to be able to log in, log out, and register. Make a new urls.py file in the users app folder. The login view is a default view provided by Django.


from django.conf.urls import url
from django.contrib.auth.views import login

from . import views

urlpatterns = [
url(r'^login/$', login,
{'template_name': 'users/login.html'},
name='login'),
url(r'^logout/$', views.logout_view, name='logout'),
url(r'^register/$', views.register, name='register'),
]

The login template
#The login view is provided by default, but you need to provide your own login template. The template shown here displays a simple login form, and provide basic error messages. Make a templates folder in the users folder, and then make a users folder in the templates folder. Save this file as login.html.
#The tag {% csrf_token %} helps prevent a common type of attack with forms. The {{ form.as_p }} element displays the default login form in paragraph format. The <input /> element named next redirects the user to the home page after a successful login.

{% extends "learning_logs/base.html" %}

{% block content %}
{% if form.errors %}
<p>
Your username and password didn't match.
please try again.
</p>
{% endif %}

<form method="post"
action="{% url 'users:login' %}">
{% csrf token %}
{{ form.as_p }}
<button name="submit">log in</button>

<input type="hidden" name="next"
value="{% url 'learning_logs:index' %}"/>
</forms>

{% endblock content %}

User accounts(cont.)

Showing the current login status
#You can modify the base.html template to show whether the user is currently logged in, and to provide a link to the login and logout pages. Django makes a user object available to every template, and this template takes advantage of this object.

#The user.is_authenticated tag allows you to serve specific content to users depending on whether they have logged in or not. The {{ user.username }} property allows you to greet users who have logged in. Users who haven't logged in see links to register or log in.

<p>
<a href="{% url 'learning_logs:index' %}"&gt;
Learning Log
</a>
{% if user.is_authenticated %}
Hello, {{ user.username }}.
&lt;  href="{% url 'user:logout' %}"&gt;
log out
</a>
{% else %}
&lt; a href="{%url 'users:register' %}"&gt;
register
</a>
&lt;a  href="{%url 'users:login' %}"&gt;
log in 
</a>
{% endif %}
</p>

{% block content %}{% endblock content %}
The logout view
#The logout_view() function uses Django'slogout() function and then redirects the user back to the home page. Since there is no logout page, there is no logout template. Make sure to write this code in the views.py file that's stored in the users app folder.

from django.http import HttpRespondRedirect
from django.core.urlresolvers import reverse
from django.contrib.auth import logout

def logout_view(request):
"""Log the user out."""
Logout(request)
return HttpResponseRedirect( reverse('learning_logs:index'))

No comments:

Post a Comment

Post Top Ad