How to create new project in django
To create a new project in Django, you can follow these steps:
Method 1: Using the Django Command Line Tool
- Open a terminal or command prompt.
- Install Django using pip:
pip install django
- Create a new project by running the following command:
django-admin startproject projectname
(replaceprojectname
with the desired name of your project). - This will create a new directory with the basic structure for a Django project.
- Navigate into the project directory:
cd projectname
- Run
python manage.py runserver
to start the development server.
Method 2: Using the Django Website
- Go to the Django website: www.djangoproject.com
- Click on the "Get Started" button.
- Follow the instructions to download and install Django.
- Once installed, click on the "Create a new project" button.
- Fill in the required information, such as project name, author name, and email address.
- Click on the "Create project" button.
- Django will generate a new project directory with the basic structure.
Basic Project Structure
A Django project typically has the following structure:
projectname/
: The top-level directory for your project.projectname/__init__.py
: An empty file that tells Python that this directory should be treated as a package.projectname/settings.py
: A file that contains settings for your project, such as database connections and installed apps.projectname/urls.py
: A file that defines the URL patterns for your project.projectname/wsgi.py
: A file that defines the WSGI application for your project.projectname/manage.py
: A command-line utility that allows you to interact with your project.
Next Steps
Once you have created a new project, you can start building your application by creating new apps, models, views, and templates. Here are some next steps:
- Create a new app:
python manage.py startapp appname
(replaceappname
with the desired name of your app). - Define your models: In the
models.py
file of your app, define your models using Django's ORM (Object-Relational Mapping) syntax. - Create views: In the
views.py
file of your app, define views that handle HTTP requests and return responses. - Create templates: In the
templates
directory of your app, create HTML templates that will be used to render your views. - Define URLs: In the
urls.py
file of your app, define URL patterns that map URLs to views.