Create new django project
Here are the steps to create a new Django project:
Step 1: Install Django
If you haven't already, install Django using pip:
pip install django
Step 2: Create a new project
Run the following command to create a new Django project:
django-admin startproject projectname
Replace projectname
with the name you want to give your project.
Step 3: Create a new app
Once the project is created, navigate into the project directory and create a new app:
python manage.py startapp appname
Replace appname
with the name you want to give your app.
Step 4: Configure the project
Open the settings.py
file in the project directory and configure the project settings as needed. For example, you can set the INSTALLED_APPS
setting to include your new app:
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'appname', # Add your app here
]
Step 5: Run the development server
Run the following command to start the development server:
python manage.py runserver
This will start the server and you can access your project at http://localhost:8000/
in your web browser.
Step 6: Create models and views
Create models and views for your app by creating files in the appname
directory. For example, you can create a models.py
file to define your models:
from django.db import models
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=100)
And a views.py
file to define your views:
from django.shortcuts import render
from.models import Book
def book_list(request):
books = Book.objects.all()
return render(request, 'book_list.html', {'books': books})
Step 7: Create templates
Create templates for your app by creating files in the appname/templates
directory. For example, you can create a book_list.html
file to display a list of books:
{% extends 'base.html' %}
{% block content %}
<h1>Book List</h1>
<ul>
{% for book in books %}
<li>{{ book.title }} ({{ book.author }})</li>
{% endfor %}
</ul>
{% endblock %}
That's it! You now have a new Django project with a new app, models, views, and templates. You can run the development server and access your project at http://localhost:8000/
in your web browser.