In this tutorial, we aim to guide you through the process of creating your first Django web application. We'll create a new app, define a model, create a view, and set up a URL for your app.
By the end of this tutorial, you will be able to:
- Set up a new Django project and app
- Create a basic model in Django
- Understand Django's MVT (Model, View, Template) architecture
- Create a simple view and map it to a URL
Prerequisites:
- Basic knowledge of Python
- Django installed on your system
django-admin startproject myproject
to create a new Django project. Replace 'myproject' with the name you want for your project.cd myproject
.python manage.py startapp myapp
to create a new app within your project. Replace 'myapp' with the name you want for your app.In Django, a model is a representation of a database table. Let's create a simple model:
models.py
inside your app directory.from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()
A view in Django is a Python function that receives a web request and returns a web response. Let's create a simple view:
views.py
inside your app directory.from django.http import HttpResponse
from .models import MyModel
def my_view(request):
my_model = MyModel.objects.first()
return HttpResponse(my_model.name)
urls.py
inside your app directory and define your URLs as follows:from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name='my_view'),
]
urls.py
, include the URLs of your app:from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')),
]
Here are the code snippets for the step-by-step guide, explained in detail:
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=100) # A character field with a maximum length of 100 characters
description = models.TextField() # A text field for longer inputs
from django.http import HttpResponse
from .models import MyModel
def my_view(request):
my_model = MyModel.objects.first() # Fetches the first entry in MyModel
return HttpResponse(my_model.name) # Returns the name attribute of the fetched model
from django.urls import path
from .views import my_view
urlpatterns = [
path('', my_view, name='my_view'), # Mapping the view to the root URL of the app
]
from django.urls import include, path
urlpatterns = [
path('myapp/', include('myapp.urls')), # Including all URLs from the 'myapp' app
]
In this tutorial, we learned how to set up a new Django project and app, define a basic model, create a simple view, and map that view to a URL. You can now create simple web applications using Django!
Next steps:
- Learn about Django's template system and create your first template
- Understand how to handle forms in Django
- Learn about Django's admin interface
Additional resources:
- Django's official documentation
- Django for Beginners
Solutions will vary depending on the specifics you chose for your project, app, model, and URL. As a tip for further practice, try to add more complex fields to your models, such as DateField or ForeignKey.