Posts

Showing posts with the label Django

Django signup

Image
 Description  We are building the customized application for user management for signup Signup In settings.py INSTALLED_APPS = [ 'polls.apps.PollsConfig' , 'user_mgmt.apps.UserMgmtConfig' , In Master urls.py from django.contrib import admin from django.urls import include , path urlpatterns = [ path( 'polls/' , include( 'polls.urls' )) , path( 'user_mgmt/' , include( 'user_mgmt.urls' )) , path( 'admin/' , admin.site.urls) , ] In app urls.py from django.urls import path from . import views app_name = 'user_mgmt' urlpatterns = [ path( 'signup' , views.signup , name = 'signup' ) , ] In views.py from django.shortcuts import render from django.contrib.auth.forms import UserCreationForm # Create your views here. def signup (request): page = 'signup' form = UserCreationForm() return render(request , 'user_mgmt/userMgmtForms.html' , { 'page' : page , ...

Django customize the admin page template

Image
 Description By default, admin page shows text django administration. We can change the existing layout.  Qqq AA . aaqa templates directory in your project directory (the one that contains manage.py). 'DIRS': [BASE_DIR / 'templates'], Now create a directory called admin inside templates, and copy the template admin/base_site.html  [dj_adm@localhost mysite]$ source ~/virtualenvs/dj-env/bin/activate (dj-env) [dj_adm@localhost mysite]$ python -c "import django; print(django.__path__)" ['/home/dj_adm/virtualenvs/dj-env/lib/python3.9/site-packages/django'] (dj-env) [dj_adm@localhost mysite]$ cd /home/dj_adm/virtualenvs/dj-env/lib/python3.9/site-packages/django (dj-env) [dj_adm@localhost django]$ ls -ltr   (dj-env) [dj_adm@localhost admin]$ pwd /home/dj_adm/virtualenvs/dj-env/lib/python3.9/site-packages/django/contrib/admin/templates/admin (dj-env) [dj_adm@localhost admin]$ mkdir -p ~/dj-practice/mysite/templates/admin/ (dj-env) [dj_adm@localhost admin]$ cp ...

Django admin page modification

Image
 Description  As per our requirement, we can tweak the admin page with models    models.py from django.db import models from django.utils import timezone import datetime # Create your models here. from django.contrib import admin class Question(models.Model): question_text = models.CharField( max_length = 200 ) pub_date = models.DateTimeField( 'date_published' ) def __str__ ( self ): return self .question_text @admin.display ( boolean = True, ordering = 'pub_date' , description = 'Published recently?' , ) def was_published_recently ( self ): now = timezone.now() return now - datetime.timedelta( days = 1 ) <= self .pub_date <= now class Choice(models.Model): question = models.ForeignKey(Question , on_delete =models.CASCADE) choice_text = models.CharField( max_length = 200 ) votes = models.IntegerField( default = 0 ) def __str__ ( self ): return self .choice_text ...

Django testcases for polls

 Description  You might have created a brilliant piece of software, but you will find that many other developers will refuse to look at it because it lacks tests; without tests, they won’t trust it. Jacob Kaplan-Moss, one of Django’s original developers, says “ Code without tests is broken by design. ”    In tests.py import datetime from django.test import TestCase from django.utils import timezone from .models import Question class QuestionModelTests(TestCase): def test_was_published_recently_with_future_question ( self ): time = timezone.now() + datetime.timedelta( days = 30 ) future_question = Question( pub_date =time) self .assertIs(future_question.was_published_recently() , False )     test polls   (dj-env) [dj_adm@localhost mysite]$ python manage.py test polls Found 0 test(s). System check identified no issues (0 silenced). ---------------------------------------------------------------------- Ran 0 tests in 0.000s OK ...

Django polls app with basics of views, url, admin and templates

Image
 Description   Django documentation  explains the application "polls". We illustrate the same with complete code. Overall flow Points to remember Whenever you create a form that alters data server-side, use method="post" In short, all POST forms that are targeted at internal URLs should use the {% csrf_token %} template tag. Always return an HttpResponseRedirect after successfully dealing with POST data. This prevents data from being posted twice if a user hits the Back button. HttpResponseRedirect takes a single argument: the URL to which the user will be redirected  Views.py rom django.shortcuts import render , get_object_or_404 # Create your views here. from django.http import HttpResponse , Http404 , HttpResponseRedirect from django.template import loader from django.urls import reverse from .models import Question , Choice from django.db.models import F def index (request): latest_question_list = Question.objects.order_by( '-pub_date' )[: 5 ] # ...

Django application templates

 Description  Render the application specific html via View   Create the templates dir dj-env) [dj_adm@localhost polls]$ mkdir templates (dj-env) [dj_adm@localhost polls]$ mkdir -p templates/polls/ (dj-env) [dj_adm@localhost polls]$ tree . ├── admin.py ├── apps.py ├── __init__.py ├── migrations │   ├── 0001_initial.py │   ├── __init__.py │   └── __pycache__ │       ├── 0001_initial.cpython-39.pyc │       └── __init__.cpython-39.pyc ├── models.py ├── __pycache__ │   ├── admin.cpython-39.pyc │   ├── apps.cpython-39.pyc │   ├── __init__.cpython-39.pyc │   ├── models.cpython-39.pyc │   ├── urls.cpython-39.pyc │   └── views.cpython-39.pyc ├── templates │   └── polls ├── tests.py ├── urls.py └── views.py 5 directories, 17 files In settings.py TEMPLATES = [ { 'BACKEND' : 'django.template.backends.django.D...

Django Models added to admin

Image
  Description  Few core live tables should  be maintained by admin. Django provides the most easiest way to attain it. start the app (dj-env) [dj_adm@localhost mysite]$ python manage.py startapp polls └── polls     ├── admin.py     ├── apps.py     ├── __init__.py     ├── migrations     │   └── __init__.py     ├── models.py     ├── tests.py     └── views.py Models.py     from django.db import models from django.utils import timezone import datetime # Create your models here. class Question(models.Model): question_text = models.CharField( max_length = 200 ) pub_date = models.DateTimeField( 'date_published' ) def __str__ ( self ): return self .question_text def was_published_recently ( self ): return self .pub_date >= timezone.now() - datetime.timedelta( days = 1 ) class Choice(models.Model): questi...

Django deploy static files

Image
 Description  Django needs to run the html, css and images from the static location that is part of our application Django.conf [root@localhost conf.d]# cat django.conf <VirtualHost *:80>     Alias /robots.txt /home/dj_adm/dj-practice/mysite/static/robots.txt     Alias /favicon.ico /home/dj_adm/dj-practice/mysite /static/favicon.ico     Alias /media/ /home/dj_adm/dj-practice/mysite/media/     Alias /static/ /home/dj_adm/dj-practice/mysite/static/     <Directory /home/dj_adm/dj-practice/mysite/static>         Require all granted     </Directory>     <Directory /home/dj_adm/dj-practice/mysite/media>         Require all granted     </Directory>     <Directory /home/dj_adm/dj-practice/mysite/mysite>     <files wsgi.py>    ...