This blogpost was originally published on my blog. You can check it out for more such posts.
Are you a Django ninja who's feeling a little rusty on the details? Or a newbie just trying to navigate the complex world of web development? Well, we've got you covered with the ultimate Django cheatsheet!
First up, let's start with some basic commands. Want to create a new project? Just type django-admin startproject projectname
into the command line. Want to start the development server? python manage.py runserver
is your new best friend.
But what about when things get a little more complex? Fear not, because we've got you covered with some handy tips and tricks.
- Want to create a new app within your project?
python manage.py startapp appname
is all you need. - Need to create a new database table? Use
python manage.py makemigrations appname
to create the initial migration, and thenpython manage.py migrate
to actually apply the changes. - Want to add a new field to an existing table? Use
python manage.py makemigrations appname
again, and thenpython manage.py migrate
as before. - Need to create a new superuser?
python manage.py createsuperuser
will prompt you for a username, email, and password.
And those are just the basics! With the ultimate Django cheatsheet by your side, you'll be able to tackle even the most complex projects with ease. So don't be afraid to get your hands dirty and dive into the world of web development. Because with Django, the possibilities are endless.
So, don't be afraid to use this cheatsheet like a pro and remember, even the most experienced Ninjas needs a little help sometimes.
Defining Django Models
Here's an example of a Django model for a Ninja
class in the style of the popular anime/manga series Naruto:
from django.db import models
class Ninja(models.Model):
name = models.CharField(max_length=255)
village = models.CharField(max_length=255)
jutsu = models.TextField()
chakra = models.IntegerField()
registered_date = models.DateTimeField(auto_now_add=True)
sharingan = models.BooleanField(default=False)
team = models.CharField(max_length=255, blank=True, null=True)
picture = models.ImageField(upload_to='ninja_images', blank=True, null=True)
def __str__ (self):
return self.name
This model defines a Ninja
class that inherits from models.Model
. It has several fields: name
, village
, jutsu
, chakra
, registered_date
, sharingan
, team
, and picture
.
The name
and village
fields are both CharField
s, with a maximum length of 255 characters. The jutsu
field is a TextField
, which can store longer pieces of text, and chakra
is an IntegerField
representing the amount of chakra a ninja has.
The registered_date
field is a DateTimeField
with auto_now_add=True
this means that it will automatically be set to the current date and time when a new Ninja
is created.
The sharingan
field is a BooleanField
which can be used to represent whether the ninja has the Sharingan or not, with a default value of False.
The team
field is a CharField
with a maximum length of 255 characters, and is optional(blank=True, null=True
) since not all ninjas are part of a team.
Finally, the picture
field is an ImageField
that will handle the upload of an image of the ninja and store it in the 'ninja_images' directory.
Defining views
Here's an example of a views.py
file for a Django application in the style of the popular anime/manga series Naruto:
from django.shortcuts import render
from django.http import HttpResponse
from .models import Ninja
def index(request):
all_ninjas = Ninja.objects.all()
context = {'all_ninjas': all_ninjas}
return render(request, 'ninjas/index.html', context)
def ninja_detail(request, ninja_id):
ninja = Ninja.objects.get(pk=ninja_id)
context = {'ninja': ninja}
return render(request, 'ninjas/ninja_detail.html', context)
def team(request, team_name):
team_members = Ninja.objects.filter(team__iexact=team_name)
context = {'team_members': team_members, 'team_name': team_name}
return render(request, 'ninjas/team.html', context)
def search(request):
query = request.GET.get('q')
search_results = Ninja.objects.filter(name__icontains=query)
context = {'search_results': search_results, 'query': query}
return render(request, 'ninjas/search.html', context)
This views.py
file contains several functions that handle different pages of the application:
- The
index
function retrieves all theNinja
objects from the database using theNinja.objects.all()
method and stores them in the context dictionary. Then it renders theindex.html
template and passes the context to it. - The
ninja_detail
function retrieves a specificNinja
object from the database using theNinja.objects.get(pk=ninja_id)
method and stores it in the context dictionary. Then it renders theninja_detail.html
template and passes the context to it. - The
team
function retrieves all theNinja
objects that belong to a specific team using theNinja.objects.filter(team__iexact=team_name)
method and stores them in the context dictionary. Then it renders theteam.html
template and passes the context to it. - The
search
function retrieves all theNinja
objects whose name contains a specific query using theNinja.objects.filter(name__icontains=query)
method and stores them in the context dictionary. Then it renders thesearch.html
template and passes the context to it.
It's important to notice that this is just an example, and you may need to adapt this views to your specific use case and also to add the URL routing for these views.
Connecting Views to URL Endpoints
Hereβs an example of how to connect the views we have defined to their endpoints
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('ninja/<int:ninja_id>/', views.ninja_detail, name='ninja_detail'),
path('team/<str:team_name>/', views.team, name='team'),
path('search/', views.search, name='search'),
]
This urls.py
file maps URLs to the corresponding views in the views.py
file:
- The empty string
''
maps to theindex
view. - The string
'ninja/<int:ninja_id>/'
maps to theninja_detail
view, and includes a variableninja_id
that is passed to the view as an argument. - The string
'team/<str:team_name>/'
maps to theteam
view, and includes a variableteam_name
that is passed to the view as an argument. - The string
'search/'
maps to thesearch
view.
You can also notice that each path has a name, this names can be used in the template to generate URLs. Also, it's important to include this urls.py file in the main urls.py file of your project with the include
function.
Rendering HTML Responses for above views
index.html
<h1>Welcome to the Ninja Database</h1>
<ul>
{% for ninja in all_ninjas %}
<li><a href="{% url 'ninja_detail' ninja.id %}">{{ ninja.name }}</a> from {{ ninja.village }}</li>
{% endfor %}
</ul>
This template displays a heading and an unordered list of all the ninjas in the database. Each list item contains a link to the ninja_detail
view for that specific ninja, using the {% url %}
template tag to generate the URL and passing the ninja.id
as the ninja_id
argument.
ninja_detail.html
<h1>{{ ninja.name }}</h1>
<p>Village: {{ ninja.village }}</p>
<p>Jutsu: {{ ninja.jutsu }}</p>
<p>Chakra: {{ ninja.chakra }}</p>
<p>Has Sharingan: {% if ninja.sharingan %}yes{% else %}no{% endif %}</p>
<p>Team: {% if ninja.team %}{{ ninja.team }}{% else %}none{% endif %}</p>
{% if ninja.picture %}
<img src="{{ ninja.picture.url }}" alt="{{ ninja.name }}">
{% endif %}
This template displays the details of a single ninja, including name, village, jutsu, chakra, Sharingan,etc.
Function Based Views
from django.shortcuts import render, get_object_or_404
from .models import Ninja
def index(request):
all_ninjas = Ninja.objects.all()
context = {'all_ninjas': all_ninjas}
return render(request, 'ninjas/index.html', context)
def ninja_detail(request, ninja_id):
ninja = get_object_or_404(Ninja, pk=ninja_id)
context = {'ninja': ninja}
return render(request, 'ninjas/ninja_detail.html', context)
def team(request, team_name):
team_members = Ninja.objects.filter(team__iexact=team_name)
context = {'team_members': team_members, 'team_name': team_name}
return render(request, 'ninjas/team.html', context)
def search(request):
query = request.GET.get('q')
search_results = Ninja.objects.filter(name__icontains=query)
context = {'search_results': search_results, 'query': query}
return render(request, 'ninjas/search.html', context)
This is similar to the previous example, the main difference is the use of get_object_or_404 function in ninja_detail view to handle cases when the ninja_id passed in the URL is not found.
It's important to notice that this is just an example, and you may need to adapt this views to your specific use case and also to add the URL routing for these views.
Class Based Views
from django.shortcuts import render
from django.views.generic import ListView, DetailView
from .models import Ninja
class NinjaListView(ListView):
model = Ninja
template_name = 'ninjas/index.html'
context_object_name = 'all_ninjas'
class NinjaDetailView(DetailView):
model = Ninja
template_name = 'ninjas/ninja_detail.html'
context_object_name = 'ninja'
class TeamView(ListView):
template_name = 'ninjas/team.html'
context_object_name = 'team_members'
def get_queryset(self):
return Ninja.objects.filter(team__iexact=self.kwargs['team_name'])
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['team_name'] = self.kwargs['team_name']
return context
class SearchView(ListView):
template_name = 'ninjas/search.html'
context_object_name = 'search_results'
def get_queryset(self):
query = self.request.GET.get('q')
return Ninja.objects.filter(name__icontains=query)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['query'] = self.request.GET.get('q')
return context
This is similar to the function-based views example, but it uses Django's built-in class-based views to handle the common logic of displaying lists and details of objects. The ListView
and DetailView
classes are generic views that handle fetching and paginating the objects from the database, and rendering the template.
It's important to notice that TeamView
and SearchView
are subclasses of ListView
and they implement the methods get_queryset
and get_context_data
to handle the specific logic of displaying a team members and search results. It's important also to notice that this is just an example, and you may need to adapt this views to your specific use case and also to add the URL routing for these views.
Forms
from django import forms
from .models import Ninja
class NinjaForm(forms.ModelForm):
class Meta:
model = Ninja
fields = ['name', 'village', 'jutsu', 'chakra', 'sharingan', 'team', 'picture']
def __init__ (self, *args, **kwargs):
super(). __init__ (*args, **kwargs)
self.fields['jutsu'].widget.attrs.update({'class': 'jutsu-selector'})
self.fields['team'].widget.attrs.update({'class': 'team-selector'})
self.fields['picture'].widget.attrs.update({'class': 'picture-input'})
class SearchForm(forms.Form):
query = forms.CharField(label='Search for a ninja', max_length=100)
team = forms.ChoiceField(choices=[('', 'All teams'), ('Team 7', 'Team 7'), ('Team 8', 'Team 8'), ('Team 10', 'Team 10'), ('Akatsuki', 'Akatsuki')], required=False)
This is an example of how to create a form to manage the data of a Ninja model. The NinjaForm
class is a ModelForm
that is created using the Ninja model and the fields specified in the fields
attribute.
The __init__
method is overriden to add classes to the widgets of the form fields, for example jutsu-selector
to jutsu field, team-selector
to team field and picture-input
to picture field.
The SearchForm
class is a regular Form
class, it's created using the CharField
and ChoiceField
form fields. The query
field is used to search for a ninja by name, and the team
field is used to filter the search results by team.
The choices
attribute of the ChoiceField
is used to specify the available teams to filter by, and the required=False
attribute is used to make the field optional, so the user can search for ninjas without filtering by team.
Django Apps
python manage.py startapp uchiha-clan
This command will create a new directory called "uchiha-clan" in your project's root directory. This directory will contain the necessary files and directories for a Django app, such as models.py
, views.py
, urls.py
, migrations/
and admin.py
.
You can then use the files in this directory to define your models, views, URLs, and other components of your app related to uchiha clan.
It's important to notice that after creating the app, you should make sure to add it to the INSTALLED_APPS
list in the settings.py
file of your project so that Django knows about the new app and can use it.
Adding an app to settings.py
Database Configuration
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'mydatabase',
'USER': 'mydatabaseuser',
'PASSWORD': 'mypassword',
'HOST': '127.0.0.1',
'PORT': '5432',
}
}
Timezone Configuration
TIME_ZONE = 'UTC'
Application Configuration
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app1',
'app2',
'app3',
]
Middleware Configuration
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
Allowed Hosts
The ALLOWED_HOSTS
setting is used to specify a list of hostnames that this Django site can serve. This is a security measure to prevent an attacker from poisoning caches and password reset emails with links to malicious hosts by submitting requests with a fake HTTP Host header, which is possible even under many seemingly-safe webserver configurations.
ALLOWED_HOSTS = ['example.com', 'www.example.com']
Static files
In production, you will also want to serve your static files using a separate web server, such as Apache or Nginx, rather than using the built-in Django development server. You'll need to configure your web server to serve the files in the STATIC_ROOT
directory.
Here's an example of how you might set STATIC_ROOT
and STATIC_URL
in your settings.py
file:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'
In addition, you might want to configure your web server to serve media files as well.
Conclusion
In conclusion, mastering the art of Django can be a daunting task, but with our ninja-inspired cheatsheet, you'll be slicing and dicing your way to the top of the leaderboard in no time! Whether you're a beginner or an expert, you'll be able to unleash the full power of the Sharingan with our easy-to-use guide. Remember, just like the Uchiha clan, you too can have the power to control the nine-tailed fox (or in this case, your code) with the right training. So what are you waiting for? Grab your kunai and get coding like a true ninja!
This blogpost was originally published on my blog. You can check it out for more such posts.
Top comments (0)