Django
FIELD TYPES: CharField TextField IntegerField DateField and DateTimeField EmailField FileField and ImageField AutoField ForeignKey ManyToManyField další typy: https://docs.djangoproject.com/en/2.1/ref/models/fields/#field-types
#CharField is used to define short-to-mid sized fixed-length strings. You must specify the max_length of the data to be stored. #TextField is used for large arbitrary-length strings. You may specify a max_length for the field, but this is used only when the field is displayed in forms (it is not enforced at the database level). #IntegerField is a field for storing integer (whole number) values, and for validating entered values as integers in forms. #DateField and DateTimeField are used for storing/representing dates and date/time information (as Python datetime.date in and datetime.datetime objects, respectively). These fields can additionally declare the (mutually exclusive) parameters auto_now=True (to set the field to the current date every time the model is saved), auto_now_add (to only set the date when the model is first created) , and default (to set a default date that can be overridden by the user). #EmailField is used to store and validate email addresses. #FileField and ImageField are used to upload files and images respectively (the ImageField simply adds additional validation that the uploaded file is an image). These have parameters to define how and where the uploaded files are stored. #AutoField is a special type of IntegerField that automatically increments. A primary key of this type is automatically added to your model if you don't explicitly specify one. #ForeignKey is used to specify a one-to-many relationship to another database model (e.g. a car has one manufacturer, but a manufacturer can make many cars). The "one" side of the relationship is the model that contains the key. #ManyToManyField is used to specify a many-to-many relationship (e.g. a book can have several genres, and each genre can contain several books). In our library app we will use these very similarly to ForeignKeys, but they can be used in more complicated ways to describe the relationships between groups. These have the parameter on_delete to define what happens when the associated record is deleted (e.g. a value of models.SET_NULL would simply set the value to NULL).
FIELD ARGUMENTS help_text verbose_name default null blank choices primary_key
#help_text: Provides a text label for HTML forms (e.g. in the admin site), as described above. #verbose_name: A human-readable name for the field used in field labels. If not specified, Django will infer the default verbose name from the field name. #default: The default value for the field. This can be a value or a callable object, in which case the object will be called every time a new record is created. #null: If True, Django will store blank values as NULL in the database for fields where this is appropriate (a CharField will instead store an empty string). The default is False. #blank: If True, the field is allowed to be blank in your forms. The default is False, which means that Django's form validation will force you to enter a value. This is often used with null=True , because if you're going to allow blank values, you also want the database to be able to represent them appropriately. #choices: A group of choices for this field. If this is provided, the default corresponding form widget will be a select box with these choices instead of the standard text field. #primary_key: If True, sets the current field as the primary key for the model (A primary key is a special database column designated to uniquely identify all the different table records). If no field is specified as the primary key then Django will automatically add a field for this purpose.
manage.py
A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in django-admin and manage.py.
mysite/__init__.py
An empty file that tells Python that this directory should be considered a Python package. If you're a Python beginner, read more about packages in the official Python docs.
mysite/wsgi.py
An entry-point for WSGI-compatible web servers to serve your project. See How to deploy with WSGI for more details.
VYTVOŘENÍ APLIKACE
CREATING APP
TVORBA MODELŮ
CREATING MODELS
VYTVOŘENÍ PROJEKTU
CREATING PROJECT
NASTAVENÍ DATABÁZE
DATABASE SETTING
vytvořená aplikace se přidá do souboru settings.py do listu: INSTALLED_APPS
INSTALLED_APPS
SPOUŠTĚNÍ SERVERU
RUNNING SERVER
mysite/settings.py
Settings/configuration for this Django project. Django settings will tell you all about how settings work.
TEMPLÁTY
TEMPLATES
editace souboru s templáty v settings.py - editace TEMPLATES: DIR TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], 'APP_DIRS': True, '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', ], }, }, ]
TEMPLATE_DIR = os.path.join(BASE_DIR,"templates") TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [TEMPLATE_DIR,], 'APP_DIRS': True, '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', ], }, }, ]
mysite/urls.py:
The URL declarations for this Django project; a "table of contents" of your Django-powered site. You can read more about URLs in URL dispatcher.
TVORBA POHLEDU
WRITING VIEW
databáze se nastavuje ve složce projekt/setting.py
defaultní databáze je SQLite pro jiná databáze: https://docs.djangoproject.com/en/2.1/topics/install/#database-installation
4. vytvoření django projektu
django-admin startproject first_project
1. vytvořit virtuální prostředí py -m virtualenv env
env...název složky, vytvořit ve složce, kde je projekt
modely se tvoří ve složce aplikace/models.py
from django.db import models class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) choice_text = models.CharField(max_length=200) votes = models.IntegerField(default=0)
Metadata řazení...ordering výsledků databáze a další metody
https://docs.djangoproject.com/en/2.1/ref/models/options/
dokumentace pro nastavení jiných databází než SQLite
https://docs.djangoproject.com/en/2.1/ref/settings/#std:setting-DATABASES
The inner mysite/ directory
is the actual Python package for your project. Its name is the Python package name you'll need to use to import anything inside it (e.g. mysite.urls).
vytvořené složky
mysite/ manage.py mysite/ __init__.py settings.py urls.py wsgi.py
kód first_app/views.py se upraví následovně: from django.http import HttpResponse def index(request): return HttpResponse("Hello, world. You're at the polls index.")
nejjednoduší pohled/view v djangu
vytvoření složky s templáty
projekt/templates/aplikace
mysite/
root directory is just a container for your project. Its name doesn't matter to Django; you can rename it to anything you like.
py manage.py runserver 0:8000
server lze spustit na jiné IP adrese (0) a jiném portu (8000)
python manage.py runserver
spouští server
2. aktivace virtuálního prostředí .\env\Scripts\activate
spustí virtuální prostředí deaktivuje se příkazem: deactivate
first_app/ __init__.py admin.py apps.py migrations/ __init__.py models.py tests.py views.py
vytvořené složky
python manage.py startapp first_app
vytvoří aplikaci
3. instalace django knihovny: pip install django
vytvoří knihovnu v projektu