Skip to content

Instantly share code, notes, and snippets.

@sandnima
Created June 19, 2021 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sandnima/6636736a0fe049c842d6cb046f933c61 to your computer and use it in GitHub Desktop.
Save sandnima/6636736a0fe049c842d6cb046f933c61 to your computer and use it in GitHub Desktop.
This Checklist help you remember what to do creating new app in Django framework

Django Check list:

Main Starter

1- Install Django: pip install django==2.1.5
2- Start Project: django-admin startproject trydjango .
3- python manage.py runserver
4- python manage.py migrate
5- python manage.py createsuperuser


Create Brand New app

Create app

  • python manage.py startapp <app_name>
  • add app_name to INSTALLED_APPS in setting.py

Create Models

  • Create class in models.py
    class <model_name>(models.Model):
        <field_name> = models.CharField(max_length=120)
        <field_name> = models.TextField()
        <field_name> = models.BooleanField(default=True)  
    
  • python manage.py makemigrations
  • python manage.py migrate
  • add from .models import <model_name> to admin.py
  • add admin.site.register(<model_name>) to admin.py
  • Go to admin panel to see databases

Urls

  • add from django.urls import include, path to urls.py in main Project Folder

  • add path('<app_name>/', include('<app_name>.urls')),

  • create urls.py file in app folder

    • add from django.urls import path to urls.py
    • add from .views import <defined>_view, <defined>_view to urls.py
    • add app_name = <app_name>
    • add path('<path>', <defined>_view, name='<defined>'),

Create Forms

  • create forms.py file in app folder
  • add from django import forms to forms.py
  • add from .models import <model_name> to forms.py
  • add form class to forms.py
    class ProductForm(forms.ModelForm):
        class Meta:
      		model = <model_name>
      		fields = [
      			'<model_field>',
      			'<model_field>',
      			'<model_field>',
      		]
    
  • add from .forms import <form_class_name> to views.py

Dynamic Linking

  • add from django.urls import reverse to models.py
  • add function to Class in models.py
    def get_absolute_url(self):
        return reverse("blog:detail", kwargs={"id": self.id})
    

Create views (Function Based)

  • put os.path.join(BASE_DIR, 'templates') to TEMPLATES > DIRS to setting.py
  • create <app_name> folder in main folder or in <app_name> folder
  • create templates folder in main folder or in <app_name> > <app_name> folder
  • add from .models import <model_name> to views.py
  • define function in views.py
    def home_view(request):
        return render(request, "home.html", {})
    
  • add html file to templates folder

Create views (Class Based)

  • add to views.py
    from django.views.generic import (
        CreateView,
        DetailView,
        ListView,
        UpdateView,
        DeleteView
    )
    
  • add from .models import <model_name> to views.py
  • add from .views import <model_name>ListView to urls.py
  • add paterns to urls.py
    app_name = '<app_name>'
    urlpatterns = [
        path('', <model_name>ListView.as_view(), name='list'),
        path('<int:id>', ArticleDetailView.as_view(), name='detail'),
    ]
    

  • add Class to views.py for creating List view
    class <model_name>ListView(ListView):
        template_name = '<app_name>/<template_name>'
        queryset = <model_name>.objects.all()
    
  • add Class to views.py for creating Detail view
    class <model_name>DetailView(DetailView):
        template_name = '<app_name>/<template_name>'
    
        def get_object(self):
            id_ = self.kwargs.get("id")
            return get_object_or_404(<model_name>, id=id_)
    
  • add from .forms import <model_name>ModelForm to views.py
  • add Class to views.py for creating Ceate view
    class <model_name>CreateView(CreateView):
        template_name = '<app_name>/<template_name>'
        form_class = <model_name>ModelForm
        
        # success_url = '/'
        def form_valid(self, form):
            return super().form_valid(form)
        
        # def get_success_url(self):
        #     return '/'
    
  • add Class to views.py for creating Update view
    class <model_name>UpdateView(UpdateView):
        template_name = '<app_name>/<template_name>'
        form_class = <model_name>ModelForm
        
        def get_object(self):
            id_ = self.kwargs.get("id")
            return get_object_or_404(<model_name>, id=id_)
        
        # success_url = '/'
        def form_valid(self, form):
            return super().form_valid(form)
        
        # def get_success_url(self):
        #     return '/'
    
  • add from django.urls import reverse to views.py
  • add Class to views.py for creating Delete view
    class <model_name>DeleteView(DeleteView):
        template_name = '<app_name>/<template_name>'
    
        def get_object(self):
            id_ = self.kwargs.get("id")
            return get_object_or_404(<model_name>, id=id_)
        
        def get_success_url(self):
            return reverse('<app_name>:<url_name>')
    
  • add from django.shortcuts import redirect to views
  • add return redirect('<url>') to View for redirecting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment