Django Beginner's Guide: Quickly Set Up Your First Website
Django Beginner's Guide: Quickly Set Up Your First Website
Django is an efficient and powerful Python web development framework suitable for building complex web applications and websites. This framework integrates many useful features, allowing developers to quickly build robust applications. Whether you are a beginner or an experienced developer, Django can provide you with many conveniences. In this article, we will teach you how to use Django to set up your first website through specific steps.
1. Environment Preparation
Before starting, ensure that your development environment has Python and pip (Python's package management tool) installed. Django requires Python version 3.6 or higher.
Install Python and pip
You can download and install the version suitable for your operating system from the Python official website. After installation, check the versions of Python and pip via the command line:
python --version
pip --version
Install Django
Installing Django via pip is very simple. Enter the following command in the command line:
pip install django
After installation, ensure that you can find the version of Django in the command line:
django-admin --version
2. Create a Django Project
Once Django is installed, we can start creating a new project. A project is the basic structure of Django, containing your applications and configurations.
Create a Project
In the directory where you want to create the project, run the following command:
django-admin startproject myproject
This will create a new directory named myproject, containing some default files and directory structure.
Directory Structure
You will see the following directory structure:
myproject/
manage.py
myproject/
__init__.py
settings.py
urls.py
asgi.py
wsgi.py
manage.py: Django's management tool used to manage the project.settings.py: The project's configuration file, where you can configure the database, static files, etc.urls.py: The project's URL routing configuration.asgi.pyandwsgi.py: Interface configurations for deployment.
3. Run the Development Server
In the project directory, use the following command to start Django's development server:
python manage.py runserver
If everything is working correctly, you should be able to access Django's welcome page in your browser at http://127.0.0.1:8000/.
4. Create Your First Application
A Django project can consist of multiple applications. Each application is responsible for a specific functional module.
Create an Application
In the myproject directory, run the following command to create an application named myapp:
python manage.py startapp myapp
This will create a new myapp directory in your project directory, structured like this:
myapp/
migrations/
__init__.py
admin.py
apps.py
models.py
tests.py
views.py
Modify settings.py
In settings.py, register your application. Find the INSTALLED_APPS list and add 'myapp',:
INSTALLED_APPS = [
...
'myapp',
]
5. Create Views and Templates
Django adopts the MVC pattern, where views and templates complement each other.
Create Views
Define a simple view in myapp/views.py:
from django.http import HttpResponse
def home(request):
return HttpResponse("Hello, Django!")
Configure URL
Create a file named urls.py in the myapp directory and set up URL routing:
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
]
Then import the myapp URLs in the project's urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('myapp.urls')),
]
6. Access Your Page
Now, you can restart the development server and visit http://127.0.0.1:8000/. You should see the message "Hello, Django!".
7. Add Database Support
Django uses SQLite by default, but also supports other databases like MySQL and PostgreSQL. Here’s how to configure SQLite.
Database Settings
In settings.py, find the DATABASES section; the default configuration looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / "db.sqlite3",
}
}
You can modify it to use another database as needed. For example, using PostgreSQL:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'yourdbname',
'USER': 'yourusername',
'PASSWORD': 'yourpassword',
'HOST': 'localhost',
'PORT': '5432',
}
}
Make sure to install the corresponding database driver; for PostgreSQL, you need to install psycopg2.
pip install psycopg2
8. Create Models and Migrate Database
Next, let's define a simple model and migrate it to the database.
Create Model
Add the following code in myapp/models.py to define a simple model:```python
from django.db import models
class Item(models.Model): name = models.CharField(max_length=100) description = models.TextField()
def __str__(self):
return self.name
### Generate Migrations
Run the following command to generate the database migration file:
```bash
python manage.py makemigrations
Then, apply these migrations:
python manage.py migrate
9. Create Admin Interface
Django comes with a powerful admin interface that makes it easy to manage data.
Create Superuser
Create an admin account using the following command:
python manage.py createsuperuser
Add Model to Admin Interface
Register your model in myapp/admin.py:
from django.contrib import admin
from .models import Item
admin.site.register(Item)
Now run the development server again, visit http://127.0.0.1:8000/admin, log in with the created superuser, and you will see the model you just registered.
Conclusion
Django is a powerful web development framework that can help you quickly build web applications. By following the methods described in this article, you can easily set up a simple Django website and expand its functionality. As you gain a deeper understanding of Django, you can explore more advanced features such as user authentication, RESTful APIs, testing, and more. I hope this beginner's guide can help you take the first step and start your Django journey!




