# Django for Web Development

### Installing Django

After installing Python, we can install django using this command `pip install django`

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700290857844/ced31749-d5cf-45c8-86af-107fc5aeaa99.png align="center")

### Creating an environment

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700292433290/1a5fa0c4-7743-4f6f-8222-acb801c618ee.png align="center")

### How to run locally  

To run the project locally, first we have to change our directory to "mysite". We can do so by `cd mysite`.  
After changing the directory we may have to migrate the python file to do so we can type in this command `python manage.py migrate`, after this we can run the server by typing in this command `python manage.py runserver`, if the server is busy or already in use we can change the port by writing this command `python manage.py runserver <port number>`.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700293101783/ea6d70fd-55b5-44bb-a934-1d6fd785ad24.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700293121260/6e1188e4-01cf-4aa9-9517-e4d32505402a.png align="center")

###   
How to create an app to work with?

To start an application we just have to write this command `python manage.py startapp <app name>` and then run server on any port which is not busy.  

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700294382716/96162f90-2e0b-47fe-9134-99081c75c324.png align="center")

###   
Routing

This is the views.py file on main application.

```python
from django.shortcuts import render # this is the default import, used for rendering templates
from django.http import HttpResponse # this is used for handling HTTP requests   

# Create your views here.

def index(response):
    return HttpResponse("<h1> This is the main app homepage </h1>")
```

we have a view function called `index`. It takes a single parameter named `resonse` (which seems to be a typo and should be `response`). This function is responsible for handling requests to the main app homepage.

Inside the `index` function, we see a single line of code that returns an `HttpResponse` object. The `HttpResponse` object is initialized with a string that contains an HTML heading `<h1> This is the main app homepage </h1>`. This means that whenever a user accesses the main app homepage, this function will be called, and the specified HTML heading will be displayed as the response.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700314208189/e0e8e085-d172-4816-b448-ce732e0c3e8e.png align="center")

The line `from . import views` is a Python code snippet from a file called [`urls.py`](http://urls.py). This line is used to import the `views` module from the current package or directory.

In Django, the [`urls.py`](http://urls.py) file is responsible for defining the URL patterns and associating them with corresponding views. The `views` module contains the view functions that handle these URL patterns.

**On mysite module we have another urls.py which contains this code**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700315606407/03cd63d2-76fa-4ff9-ab7c-5eb8a3eb5f9a.png align="center")

1. The first URL pattern is `path('admin/',` [`admin.site`](http://admin.site)`.urls)`. This pattern maps the URL `admin/` to the Django admin interface. When a user accesses this URL, the admin interface will be displayed.
    
2. The second URL pattern is `path('', include('main.urls'))`. This pattern maps the root URL (empty string) to the URLs defined in the `main.urls` module. The `include` function is used to include the URL patterns from another module (`main.urls`) into the current URL configuration. This allows for modular and reusable URL patterns.
    

This is how we can render anything locally.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1700315664413/30a38ddc-e15e-417c-8d46-6156875e54b5.png align="center")
