H a n d s o n, p r o j e c t b a s e d


Download 4.21 Mb.
Pdf ko'rish
bet327/344
Sana31.01.2024
Hajmi4.21 Mb.
#1818553
1   ...   323   324   325   326   327   328   329   330   ...   344
Bog'liq
Python Crash Course, 2nd Edition

421
try It yoUrself
19-1. Blog:
Start a new Django project called Blog. Create an app called blogs 
in the project and a model called BlogPost. The model should have fields like 
title
, text, and date_added. Create a superuser for the project, and use the 
admin site to make a couple of short posts. Make a home page that shows all 
posts in chronological order.
Create a form for making new posts and another for editing existing posts. 
Fill in your forms to make sure they work.
Setting Up User Accounts
In this section, we’ll set up a user registration and authorization system so 
people can register an account and log in and out. We’ll create a new app 
to contain all the functionality related to working with users. We’ll use the 
default user authentication system included with Django to do as much 
of the work as possible. We’ll also modify the 
Topic
model slightly so every 
topic belongs to a certain user.
The users App
We’ll start by creating a new app called 
users
, using the 
startapp
command:
(ll_env)learning_log$ python manage.py startapp users 
(ll_env)learning_log$ ls 
u
db.sqlite3 learning_log learning_logs ll_env manage.py users
(ll_env)learning_log$ ls users 
v
__init__.py admin.py apps.py migrations models.py tests.py views.py
This command makes a new directory called users u with a structure 
identical to the 
learning_logs
app v.
Adding users to settings.py
We need to add our new app to 
INSTALLED_APPS
in settings.py, like so:
--snip--
INSTALLED_APPS = [
# My apps
'learning_logs', 
'users',
# Default django apps.
--snip--
]
--snip--
settings.py



Download 4.21 Mb.

Do'stlaringiz bilan baham:
1   ...   323   324   325   326   327   328   329   330   ...   344




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling