Here you can find two applications, first one is a simple django based calculator (using forms and POST requests), and the second app is TRAVELLO website.
$ python manage.py makemigrations my_app
$ python manage.py makemigrations travello
$ python manage.py migrate
- my_app = the name of app
- my_project = the name of project
- python manage.py help
- Creating a virtual environment and creating a Django project:
python -m venv my_env
.\my_env\Scripts\activate
pip install Django
django-admin startproject my_project
python manage.py startapp my_app
- include my_app/urls.py in the ROOT urls.py
- Create migratioins
python manage.py migrate
python manage.py sqlmigrate my_app 0001
python manage.py makemigrations my_app
[os.path.join(BASE_DIR, 'templates')],
- After change my_app/views.py for rendering 'index.html'
return render(request, 'index.html')
- in index.html then go to my_app/urls.py add path('add', views.add, name='add'), then in views.py describe the function and render.html:
def add(request):
val1=request.POST["num1"]
val2=request.POST["num2"]
res=int(val1)+int(val2)
return render(request, 'result.html', {'result':res})
- in index.html do not forget to include {% csrf_token %} for security
INFO! MVT/MTV Model-View-Template, Data-Logic-Layout -- We have Model(DataBase) and Template(with DTL Django template language), data from user comes as object and who will link this? urls is linking, so the main logic is in the views.
- static file creation in settings.py
STATIC_URL='/static/'
STATICFILES_DIRS=[os.path.join(BASE_DIR, 'static')]
STATIC_ROOT=os.path.join(BASE_DIR, 'assets')
==>OR<==
STATIC_URL = '/static/'
STATIC_DIR = os.path.join(BASE_DIR, 'static')
STATICFILES_DIRS = [STATIC_DIR]
python manage.py collectstatic
- change the directory and names in .html files:
{% load static %}
<img src="{% static "my_app/example.jpg" %}" alt="My image">
- Django ORM->App<-->DataBase is being created automaticly
- Download PostgreSQL and PGAdmin 4: Password: Rr87654321 and 0999
- then create a database in PG admin, do not forget NAME!
- go to settings.py->DATABASES change into
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'telusko',
'USER': 'postgres',
'PASSWORD': 'Rr87654321',
'HOST': 'localhost',
}
}
- how to connect Python with PostgreSQL? pip install psycopg2
- THEN CREATE A MODEL IN models.py CharField, ImageField ...
- add an app in list of INSTALLED_APPS
python manage.py makemigrations
python manage.py sqlmigrate my_app 0001
python manage.py migrate
- Haha, Good Job :) Do not forget to register models in admin.py:
from .models import [ClassName-it is ur model]
admin.site.register(ClassName)
python manage.py createsuperuser
3. For the Images we must create a path for media, it is the location of media file added from Admin panel
``` python
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
- in root urls.py add following
from django.conf import settings
from django.conf.urls.static import static
urlpatterns = urlpatterns + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
- in .html we must use smth.smth.url in the end
- travello/views.py
from django.shortcuts import render
from travello.models import Destination
def index(request):
dests = Destination.objects.all()
return render(request, "index.html", {'dests' : dests} )
<form action="register" method="POST">
{% csrf_token %}
<input type="text" name="first_name" placeholder="First name">
... ...
</form>
- ORM of Django has the build in model for User-> accounts/views.py
from django.contrib.auth.models import User, auth
if request.method=='POST':
first_name = request.POST['first_name']
... ...