Back to the page

In the print window, choose “Save as PDF”.

doubtly doubtly.in

How to Create a Django App

Projects · Updated 3 Apr 2024 · Read online: www.doubtly.in/how-to-create-a-django-app/

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:

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:

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:

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:

We hope this blog post has been helpful. If you have any questions, please feel free to leave a comment below.

Practise this on Doubtly (free)