Presentation is loading. Please wait.

Presentation is loading. Please wait.

Django Users and Registration

Similar presentations


Presentation on theme: "Django Users and Registration"— Presentation transcript:

1 Django Users and Registration

2 Final Projects Starting projects tomorrow!
Each group will meet with us after lecture tomorrow

3 Today Users in Django apps

4 Users Keep track of each user’s data Keep track of permissions Name
Shopping cart Keep track of permissions

5 How websites keep track of users

6 Requesting a page Show me google.com here’s the HTML of the page
What is the problem with this? Your computer Facebook’s server 6

7 Requesting a page Show me my Facebook.com newsfeed Facebook’s server
What is the problem with this? Your computer Facebook’s server 7

8 Requesting a page Show me my Facebook newsfeed I am Leah Alpert!
Here’s the HTML of your Facebook news feed What is the problem with this? Your computer Facebook’s server 8

9 Cookies Keeps track of this data Sites can set and read cookies

10 Requesting a page, with cookies
Submit login form to Facebook.com Here’s a cookie. username = leahalpert What is the problem with this? Your computer Facebook’s server 10

11 Requesting a page, with cookies
Show me Facebook.com. Here’s my cookie: username = leahalpert Here’s the HTML of your Facebook news feed What is the problem with this? Your computer Facebook’s server 11

12 Cookies Website needs to send these cookies back and forth on every request

13 Django can do this for you!
User database Track what user is logged in on each computer

14 User Database Already has a class model.User Fields: Methods: username
firstname lastname password Methods: is_authenticated() get_full_name() set_password() _user()

15 See and add users

16 Settings INSTALLED_APPS = ( ‘blog’, … ‘django.contrib.sessions’,
‘django.contrib.auth’, )

17 Track what user is logged in
Let users log in and out Know which user is logged in

18 Login form class LoginForm(forms.Form): username = forms.CharField()
password = forms.CharField(widget=forms.PasswordInput)

19 Get data from form and put it in the database
Models Template View User Get data from form and put it in the database

20 Login form When submit is pressed, sends to view:
{username: leahalpert, password: abc123}

21 View to handle login def login_view(request):
uname = request.POST[‘username’] pass = request.POST[‘password’] user = authenticate(username=uname, password=pass) if user is not None: login(request, user)

22 logout(request) def logout_view(request): logout(request)

23 Access information about the current user in the views
def blog_list(request): ... Request holds information that browser sent to server Access information with request.user request.user.username request.user.

24 Referencing the user in a template
{% if user.is_authenticated %} <h2>Welcome, {{user.username}}! </h2> {% else %} <h2>Welcome. Please log in.</h2> {% endif %}


Download ppt "Django Users and Registration"

Similar presentations


Ads by Google