Locallibrary application with Django This code pratice follows the tutorial of MDN Web Docs
session
attribute can be accessed from therequest
parameter.
# Get a session value by its key (e.g. 'my_car'), raising a KeyError if the key is not present
my_car = request.session['my_car']
# Get a session value, setting a default if it is not present ('mini')
my_car = request.session.get('my_car', 'mini')
# Set a session value
request.session['my_car'] = 'mini'
# Delete a session value
del request.session['my_car']
def index(request):
...
num_authors = Author.objects.count() # The 'all()' is implied by default.
# Number of visits to this view, as counted in the session variable.
num_visits = request.session.get('num_visits', 0)
request.session['num_visits'] = num_visits + 1
context = {
'num_books': num_books,
'num_instances': num_instances,
'num_instances_available': num_instances_available,
'num_authors': num_authors,
'num_visits': num_visits,
}
# Render the HTML template index.html with the data in the context variable.
return render(request, 'index.html', context=context)
path('accounts/', include('django.contrib.auth.urls'))
adds the following URLs with names in square brackets:
accounts/ login/ [name='login']
accounts/ logout/ [name='logout'] #default template name 'logged_out.html'
accounts/ password_change/ [name='password_change']
accounts/ password_change/done/ [name='password_change_done']
accounts/ password_reset/ [name='password_reset'] #password_reset_form.html
accounts/ password_reset/done/ [name='password_reset_done'] #password_reset_done.html
accounts/ reset/<uidb64>/<token>/ [name='password_reset_confirm'] #password_reset_confirm.html
accounts/ reset/done/ [name='password_reset_complete'] #password_reset_complte.html
- Authentication and permissions(Authorization)
Model :Defining permissions is done on the model "class Meta" section, using the permissions field.
Template:The current user's permissions are stored in a Template variable called {{ perms }}
Views:Django use decorator
(login_required
, permission_required
) in function base view to implement the authentication and permission while use mixins
(LoginRequiredMixin
, PermissionRequiredMixin
)in class base view.
#function base view
from django.contrib.auth.decorators import login_required, permission_required
@login_required
@permission_required('catalog.can_mark_returned')
@permission_required('catalog.can_edit')
def my_function(request):
pass
#class base view
from django.contrib.auth.mixins import LoginRequiredMixin, PermissionRequiredMixin
class MyClassView(LoginRequiredMixin,PermissionRequiredMixin,View):
permission_required = ('catalog.can_mark_returned','catalog.can_edit')
pass
-
admin.py
can control thelist_display
andlist_filter
in admin site bydecorators
. -
Forms variables in Template:
`{{ form.as_table }}` will render them as table cells wrapped in <tr> tags
`{{ form.as_p }}` will render them wrapped in <p> tags
`{{ form.as_ul }}` will render them wrapped in <li> tags
- Write automated tests.
Unit tests
: Verify functional behavior of individual components, often to class and function level.setUpTestData()
is called once at the beginning of the test run for class-level setup. You'd use this to create objects that aren't going to be modified or changed in any of the test methods.setUp()
is called before every test function to set up any objects that may be modified by the test (every test function will get a "fresh" version of these objects).
The most essential thing in writing a unit test is focusing on the desired design in code, not the Django built-in function or third-party libraries.
One worth to mention is the relationship between some test functions may be recuresive. For example ,fun1()
contains step A
and step B
, while fun2()
may contains step A
, step B
and step C
.
- Before deploying the application, we need to:
- Make a few changes to your project settings.
- Choose an environment for hosting the Django app.
- Choose an environment for hosting any static files.
- Set up a production-level infrastructure for serving your website.
python manage.py check --deploy
can automatically check settings security before deployment.
- Set up the database configuration with
dj_database_url
insettings.py
# Heroku: Update database configuration from $DATABASE_URL.
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
reverse(viewname, urlconf=None, args=None, kwargs=None, current_app=None)
VSreverse_lazy()
reverse
reverse the view name then return a URL in URLconf
.
While reverse_lazy
is useful for when you need to use a URL reversal before your project’s URLConf is loaded.
-
Researches on online library need to done before fully designing website features. -- Product Functions:
- A new user can register [First name , Last name , email, personal decription]and has its own profile and so on.
- automatically check the status of book instance,manage the borrowed books with due date, list them or remind the borrower with a email .
- set the limitation number of books a borrower can take.
-
Need a good design for the website.
- how the
session
will be used in more complicated ways. - Writing
Unit Test
seems to be a boring and tedious job which may take much time. How it would be in real production?