Before starting here if you are familiar with creating Django project feel free to skip this tutorial and start the next.
If not just make sure you have installed python and Django in your system by follow command, if you want more detail follow Django Website
pip install django
above command will install Django application in system, now we need to create project directory where we can store our code and manage to run, copy below code, and run in your CMD/Terminal
django-admin startproject langtests
This will create the langtests directory in the current working directory. To create the languages app, cd into langtests and execute:
python3 manage.py startapp languages
The directory structure of our project will be the following:
Here, langtests contains 1 manage.py file, one sub directory which contains 4 python files with extension .py file such as settings.py, urls.py, wsgi.py and _init.py. and last i.e. third one is 2nd sub folder which is our application i.e., languages, this contains 5 python files and one folder with name migrations, migrations folder store all db file info will discuss this in other post, now look at 5 diff files namely __init_.py, admin.py, models.py, tests.py, and views.py,
Overview of each file:
_init_.py: An empty file that tells Python that this directory should be considered a Python package.
settings.py: Contains all the information of projects such as Database, Template, Middleware, Third Party, i18N, Static information and many more. Settings/configuration for this Django project. Django settings will tell you all about how settings work.
urls.py: The URL (Uniform Resource Locator) declarations for this Django project; a “table of contents” of your Django-powered site. You can read more about URLs (Uniform Resource Locators) in URL dispatcher.
wsgi.py: An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
If you have worked with Django, you already know what these files are. If not, I recommend following the Django tutorial as it will allow you to understand most of the basic concepts.
Let get touch some code introductory code part:
Let create a simple view for our 'languages' app's index page. Open languages/views.py and paste the code below:
from django.http import HttpResponse def index(request): output = 'Welcome to my site.' return HttpResponse(output)
This view will now be mapped to a URL. Created a file named urls.py in the languages directory that contains the following code:
from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), ]
Finally, from the root URLconf, map a URL. Include the URLs for the language apps in urlpatterns in langtests/urls.py as follows:
from django.conf.urls import include from django.contrib import admin from django.urls import path urlpatterns = [ path('admin/', admin.site.urls), path('languages/', include('languages.urls')), ]
To test the view, start the Django server using below command
python3 manage.py runserver
and navigate to http://localhost:8000/languages/ in your browser.
Disclaimer: This is a personal [blog, post, statement, opinion]. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally.