Project with python language and django framework ⊹˚. ♡.𖥔 ݁ ˖ (Initial Settings)
Create a virtual environment in Python, separating the interpreter, so there is no confusion of versions with other projects:
python3 -m venv venv (second venv is random name)
python -m venv venv (second venv is random name)
source venv/bin/activate
venv\Scripts\Activate
If there is an error, and you are using windows, the Poweshell needs to accept permissions for execute scripts, so use this and try again:
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned
pip is Python's package manager, and through pip, you install the libraries The Pillow library is a Python image processing library that offers a number of functionalities for processing
django-admin startproject pystack . (pystack is the name given to the new Django project and the dot at the end specifies the directory in which the project should be created. In this case, it is the current directory)
You will see something like this: Starting development server at http://127.0.0.1:8000/ Do CTRL+click on https
࣪𖤐 Observation: I advise you to research what Django's communication layer is like, you can start with MVT (Model, Views and Templates)
Python manage.py StartApp Users (You can change the name Users) When you create a new folder (a new app) you will need to go to the application's Core (pystack) and go to the settings so that Django recognizes this folder as one of its apps. From there, you'll need to insert this new users folder into the constant INSTALLED_APPS=[ ] INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'users', 👈🏻 ]
path('users/', include('users.urls')), (This include('users.urls') exists because within the users route, there may be other routes such as registration or login, so you INCLUDE it so that it can also search for these routes when it is inside the users route, Find within the Users app and within the URL file)
Go to users -> create a file urls.py (This is where we register the URLs and the url users find the others urls)
In the APP users create a URL for registration: from django.urls import path from . Import Views urlpatterns = [ (Inside urlpatterners Django will look for other URLs within users) path('registration/', views.registration, name='registration'), (This views.registration is the function we'll call to process the registration page, so we import the from . import views) the dot is a current ]
Also, we need the view to process the files within the registration url. So go to users -> views.py where we will have all the functions that will be used for processing
from django.shortcuts import render from django.http import HttpResponse
def registration(request): return HttpResponse("Hello world!")
http://127.0.0.1:8000/users/registration/ Make sure the "Hello World!" message is there
import os (Is used to import the operating system module into Python. The OS module provides an interface for accessing and manipulating operating system resources such as directories, files, processes, and more)
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [os.path.join(BASE_DIR, 'templates')], (os.path.join() It is used to concatenate directories independent OS and the (BASE_DIR) is a constant, references the path from the root of the computer to the root of our project and the 'templates' is the name of directory that you will create) 'APP_DIRS': True, (If true, Django will also look for 'templates' within the app, that is, it can create a 'templates' folder where you can create HTML files within folder users) 'OPTIONS': { 'context_processors': [ 'django.template.context_processors.debug', 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', ], }, }, ]
Once you have created the templates folder inside the users folder, you will create an html file inside that templates folder
from django.shortcuts import render from django.http import HttpResponse def registration(request): return render(request, 'registers.html')
Finally, when we have templates made in figma or others and we realize that there are many colors that are repeated, codes and styles, we will do this:
And inside this base file, you put the common characteristics of each page that your project will have
{% load static %} <!doctype html>
<title>Pystack</title> {% block 'header' %} {% endblock %} {% block 'content' %}{% endblock %} <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>These lines of code are standard settings for html files, such as bootstrap...But {% block 'header' %} {% endblock %} and {% block 'content' %}{% endblock %} are blocks mean that there will be another html file that will extend this base and insert new html code inside these blocks
Inside the registers file.html you will put: {% extends "base.html" %} to this file to inherit all the characteristics of the base base.html
Finally, you'll put {% block 'content' %} and then {% endblock %}. Anything within that will be applied to all page content
When this is done, you won't need to repeat html code, just extend the base and put the code inside {%%}. Remembering that the codes you put INSIDE will be applied to the line of code that is in the base.html
That was the initial configuration file for my project using the django framework! Follow the same steps to create your own ☆ ★ ✮ ★ ☆☆ ★ ✮ ★ ☆