[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

warisali2/django-cheatsheet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 

Repository files navigation

django-cheatsheet

Django Cheatsheet

Table Of Contents

Check Django Version

python -m django --version

Create Django Project

Note: Don't use - in project name

django-admin startproject your_project_name

It creates following directory structure

└── your_project_name
    ├── manage.py
    └── your_project_name
        ├── __init__.py
        ├── settings.py
        ├── urls.py
        └── wsgi.py

These files are:

  • Outer your_project_name Root director of your project. Just a container.
  • manage.py Django utility to interact with project.
  • Inner your_project_name Actual django app.
  • settings.py Settings for this django project.
  • urls.py URLs declarations for this django project.
  • wsgi.py Entry point for WSGI (Web Server Gateway Interface) compatible servers to serve your project.

Run Development Server

python manage.py runserver [ip-address:port]

If no address is given, it will run server on http://localhost:8000/.

Create an App

python manage.py startapp your_app_name

It creates following directory structure

├── your_app_name
    ├── admin.py
    ├── apps.py
    ├── __init__.py
    ├── migrations
    │   └── __init__.py
    ├── models.py
    ├── tests.py
    └── views.py

Note: Apps can live anywhere on your python path.

Projects vs Apps

An app is a Web application that does something – e.g., a Weblog system, a database of public records or a simple poll app. A project is a collection of configuration and apps for a particular website. A project can contain multiple apps. An app can be in multiple projects.

Sample View

Here is simplest possible view in django

from django.http import HttpResponse

def index(request):
    return HttpResponse("Hello django!!")

Map View to URL

Create a urls.py file in your app's directory and add following lines

from django.urls import path

from . import views

urlpatterns = [
    path('url_to_view', 'your_view', name='name_for_your_url')
]

name attribute is used to refer your view from somewhere else in your code. Even if you change the actual url, name will still refer to same view. For example

In Code

password_url = reverse('name_for_your_url')

In template

<p>Please go <a href="{% url 'name_for_url' %}">here</a></p>

Point Root URLConf at App's URL module

Add following lines in your project/urls.py

from django.urls import include

urlpatterns = [
    path('sub_url_for_your_app', include('your_app.urls'))
]

include() allows referencing other URLConfs

Releases

No releases published

Packages

No packages published