First Django project. Part 1
In this article, we’ll launch a web page in 13 easy steps and take a VERY brief introduction to Django. This article is intended for people who already know a little about Python and got acquainted with the publication “What is Django?” https://habr.com/ru/articles/747234/. The article will describe for PyCharm, because it is free, easy to set up, undemanding for hardware and there are a bunch of color themes.
For experienced commentators. This is my vision. If I started studying, I would study in this way. But thanks for every comment
Briefly about what I sit on:
Ноутбук Acer ES1 520
Процессор AMD A6-7310 APU with AMD Radeon R4 Graphics, 4x2 ГГц
Память 11171 Мб
Операционная система Linux Lite 6.4
Дополнительный монитор ViewSonic VX2240W VS11869, 22 LCD
Беспроводная мышь и беспроводная удобная клавиатура
Python and PyCharm must be installed to work. Launch Pycharm and create a project.
The following 5 items in the PyCharm terminal:
For Windows, you need to create and activate a virtual environment. Who knows how to do this exactly write in the comments.
pip install django
django-admin startproject mysite
cd mysite
python3 manage.py startapp shopapp
Open file settings.py in the mysite/mysite folder and add ‘shopapp’ to the list of installed applications:
INSTALLED_APPS = [ ... 'shopapp', ... ]
In the shopapp folder, create a templates folder (necessarily “templates”). In this templates folder, create a shopapp folder (name after the application name from point 6). In this shopapp/templates/shopapp/ folder, create an index.html file. Delete the old code and add the code inside the file:
<!DOCTYPE html> <html> <head> <title>{% block title %}Главная страница{% endblock %}</title> </head> <body> <header> <h1>Мой интернет-магазин</h1> </header> <div class="content"> {% block content %} {% for product in products %} <p>{{ product }}</p> {% endfor %} {% endblock %} </div> <footer> <p>Все права защищены</p> </footer> </body> </html>
In file views.py in the shopapp/ folder, add the following code:
from django.shortcuts import render def index(request): context = { 'products': ['Товар 1', 'Товар 2', 'Товар 3'], } return render(request, 'shopapp/index.html', context)
Create a file urls.py in the shopapp/ folder and add the following code:
from django.urls import path from . import views urlpatterns = [ path('', views.index, name="index"), ]
Open file urls.py in the mysite/mysite folder. Delete the old code and add the following code:
from django.contrib import admin from django.urls import include, path urlpatterns = [ path('admin/', admin.site.urls), path('', include('shopapp.urls')), ]
Now you can launch your site with the command in the terminal:
python manage.py runserver
You should see a message that the server is running. Now you can open your browser and navigate to http://127.0.0.1:8000/ to see the main page of your online store.
Those who are interested can play with the Cyrillic alphabet in index.html and the list of products in views.py.
The first Django project will be released soon. Part 2. ”, where in the screenshots it is clear, short and simple, there will be explanations of each line and the dependence of the lines on each other.