Get Started With Django

Things to do after Starting Your Django Project

by
published on

You have created your first django project, but don't know what to do next. Here are the things you need to do after creating your django project. In this post, I'll be sharing the settings you need to configure in order to perform some basic operations in your Django Project.

First of all, create a Django Project, if you haven't done it yet.

$ django-admin startproject mysite .

This will create a django project in your current directory.

 Configure your Settings to Use HTML Templates

Locate the following code in your settings.py

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]

Add this item to the DIRS list.

'DIRS': [BASE_DIR + "templates"],

So, what we are doing is we are telling to our django that our templates are located in the directory called templates which is right in our root project directory. The line BASE_DIR + "templates" returns a string which is the path to our root project directory + "templates".

For example our project is located at C:\Users\Dev\Desktop\mysite, The Django Template Loader will try to find templates at C:\Users\Dev\Desktop\mysite\templates. This allows us to dynamically generate the path to our templates rather than a hardcoded path which makes our project portable.

A side note. Follow me on Instagram, to learn about Full Stack Web Development.

Serving Images, CSS and JavaScript With Django

Just like HTML templates, we can also serve CSS and JS files using Django. You just need to configure the proper settings for that. This files are known as static files in django. So we need to tell Django, where our static files are located.

At the end, of your settings.py file you'll find this line
STATIC_URL = '/static/'

You need to add the following line below it.
STATICFILES_DIRS = [BASE_DIR + 'static']

Now, you need to add all your CSS, JavaScript and CSS files in the static directory. Make sure your static directory is located right under your project's root directory.

Style Your Django Forms

You may have used forms generated with Django in your templates but they are not that User-Friendly. You can style your Django Forms by installing a third party package called django-crispy-forms

$ pip install django-crispy-forms

Now, add it to your Installed Apps list in settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # Add this to your Installed Apps
    'crispy_forms'
]

Now just add this line anywhere in your settings.py

CRISPY_TEMPLATE_PACK = 'boostrap4'

For more template packs check out the django-crispy-forms docs. 

A sample HTML template

{% load crispy_forms_tags %}
<!-- Don't forget to load crispy_forms in your template -->
{% extends "base.html" %}
{% block content %}
<form method="POST">
{{ form | crispy }}
<!-- Add the crispy filter to django forms -->
<button type="submit" class="btn btn-primary">Submit</button>
</form>
{% endblock content %}
That's all about this Post. Stay Tuned For The Next Post. Happy Coding. You can DM me on Instagram or email me if you have any queries.

Comments