Django is a popular Python web framework that makes it easy to build web applications. In this blog post, we will show you how to create a Django app with all options like in the same folder.
Prerequisites
Before you begin, you will need the following:
- Python 3.6 or later
- Django 3.0 or later
- A text editor or IDE
Creating a New Django Project
To create a new Django project, open your terminal or command prompt and run the following command:
if django is not installed
pip install django
django-admin startproject myproject
This will create a new directory called myproject
that contains the following files:
manage.py
: The Django management scriptmyproject
: The Django project configuration filesettings.py
: The Django settings fileurls.py
: The Django URL configuration filewsgi.py
: The Django WSGI configuration file
Creating a New Django App
To create a new Django app, run the following command:
python manage.py startapp myapp
This will create a new directory called myapp
that contains the following files:
__init__.py
: The Python package initialization fileadmin.py
: The Django admin configuration fileapps.py
: The Django app configuration filemigrations
: The Django migrations directorymodels.py
: The Django models filetests.py
: The Django tests fileviews.py
: The Django views file
Adding the App to the Project
To add the app to the project, open the settings.py
file and add the following line to the INSTALLED_APPS
list:
'myapp',
Running the Development Server
To run the development server, run the following command:
python manage.py runserver
This will start the development server on port 8000. You can now visit http://localhost:8000 in your browser to see the Django welcome page.
Creating a Model
To create a model, open the models.py
file in your app directory and add the following code:
from django.db import models
class MyModel(models.Model):
name = models.CharField(max_length=200)
This will create a new model called MyModel
with a single field called name
.
Creating a View
To create a view, open the views.py
file in your app directory and add the following code:
from django.shortcuts import render
def my_view(request):
return render(request, 'my_template.html')
This will create a new view called my_view
that renders the my_template.html
template.
Creating a Template
To create a template, create a new file called my_template.html
in your app directory and add the following code:
<h1>My Template</h1>
This will create a new template called my_template.html
that displays the text “My Template” in a heading.
Running the Development Server Again
To run the development server again, run the following command:
python manage.py runserver
This will restart the development server and allow you to see the changes you have made.
Conclusion
In this blog post, we have shown you how to create a Django app with all options like in the same folder. We covered the following topics:
- Creating a new Django project
- Creating a new Django app
- Adding the app to the project
- Running the development server
- Creating a model
- Creating a view
- Creating a template
We hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.