[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/We2one/Notes/master/python-basic-notes/python-days/Day_39.md [Back]  [Original]

### Django #### Django 1. 1. , 2. 3. 2. 1. , 2. #### Django ##### Django ( 2.2.1 ) 1. anaconda 1. : `conda create -n python=3.7` 2. : `activate ` 3. : `deactivate` 4. : `conda env list` 2. Django : `pip install django==2.2.1 -i ` ##### 1. 1. 2. 3. : `activate DjangoPath` 4. : `django-admin startproject ` 2. Django 1. Django manage.py 2. 3. + `python manage.py runserver` + `python manage.py runserver 9000` : + `python manage.py runserver 0.0.0.0:9000` : + setting.py `ALLOWED_HOST =['*']` 3. + + + `__init__.py` : + `settings.py` : + `urls.py` : + `wsgi.py` : + `db.sqlit3` : sqlite3 + `manage.py` : , ##### Django + `views.py` ```python from django.shortcuts import HttpResponse def hello(request): return HttpResponse('hello word') ``` + 1. /hello/ 2. Django `setting.py` ROOT_URLCONF , URL 3. Django URL URL , /hello/ 4. 5. HttpResponse ##### Django + ```python from django.contrib import admin from django.urls import path from .views import * urlpatterns = { path('admin/', admin.site.urls), path('hello/', hello) } ``` + 1. `path("", )` 2. `re_path("", )` + Django + `re_path(r'^\w/$', views.zf)` : \w + `re_path(r'^\w+/$', views.zf)` : + + `re_path(r'^\d/$', views.blog)` : + + url , + () , + + + (?P) #### Django ##### + manage.py templates HTML + setting.py ```python TEMPLATES = [ { ... 'DIRS' : [os.path.join(BASE_DIR, 'templates')], ... } ] ``` + HTML + views.py ```python from django.shortcuts import HttpResponse, render_to_response def hello(request): # return HttpResponse('hello word') return render_to_response('hello.html') ``` ##### + + : `{{ }}` + 1. ```python def index(request): uer_info = {'name': "", 'age': 12} hobbies = ['', ''] return render(request, 'index.html', { 'user_info': user_info, 'hobbies': hobbies, }) ``` 2. locals() ```python def index(request): uer_info = {'name': "", 'age': 12} hobbies = ['', ''] return render(request, 'index.html', locals()) ``` + : + + + + : **{% %}** + 1. if ```jinja2 {% if %} {% elif %} {% endif %} ``` 2. ifequal ```jinja2 {% ifequal age 19 %}

{% endifequal %} ``` 3. for + forloop : , if 4. for ... empty ```jinja2 {% for i in arr %} {{ i }} {% empty %} i {% endfor %} ``` 5. autoescape off ```jinja2 {% autoescape on %} {{ url }} {% endautoescape %} {% autoescape off %} {{ url }} {% endautoescape %} ``` + + : `{{ name|: }}` ##### 1. 1. manage.py static , JSCSS 2. setting.py ```python STATIC_URL = '/static/' STATICFILES_DIRS = [ os.path.join(BASE_DIR, 'static'), ] ``` 3. html ```html xxx.png [xxx.png]" ``` 2. 1. `{% load static %}` 2. `{% static 'xxx.png' %}` ##### 1. `extends` + ,AJAX + base ```jinja2 {% extends 'base.html' %} {% block main %} {% endblock %} ``` 2. `include` + ,, + ```jinja2 {% include 'navbar.html' %} ``` #### Django App + Project () App () , + App () Django , ##### App 1. `python manage.py startapp app_name` 2. `setting.py` INSTALLD_APPS App ```python INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'blog', ] ``` 3. pycharm **New Project** --> **more setting** App #### Django ##### ORM 1. Object Relational Mapping (ORM ) : . () () 2. ORM 1. Python 2. Python 3. Python 3. ORM + ORM , SQL , .. 4. ORM + ##### DjangoOrm + Django App models ##### 1. MySQL 1. ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.mysql', 'NAME': 'db_name', 'USER': 'user_name', 'PASSWORD': 'user_password', 'HOST': 'HOST_IP', 'PORT': 'mysql_port', # 3306 } } ``` 2. PyMySQL , `__init__.py` ```python import pymysql pymysql.install_as_PyMySQLdb() ``` 3. : Django 2.2.1 PyMySQL 1. ``` Traceback (most recent call last): File "manage.py", line 21, in main() File "manage.py", line 17, in main execute_from_command_line(sys.argv) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line utility.execute() File "E:\Anaconda\envs\mydjango\lib\site-packages\django\core\management\__init__.py", line 375, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv self.execute(*args, **cmd_options) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\core\management\base.py", line 364, in execute output = self.handle(*args, **options) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\core\management\base.py", line 83, in wrapped res = handle_func(*args, **kwargs) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\core\management\commands\makemigrations.py", line 101, in handle loader.check_consistent_history(connection) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\migrations\loader.py", line 283, in check_consistent_history applied = recorder.applied_migrations() File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\migrations\recorder.py", line 73, in applied_migrations if self.has_table(): File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\migrations\recorder.py", line 56, in has_table return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\base\base.py", line 256, in cursor return self._cursor() File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\base\base.py", line 233, in _cursor self.ensure_connection() File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\base\base.py", line 217, in ensure_connection self.connect() File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\base\base.py", line 197, in connect self.init_connection_state() File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\mysql\base.py", line 231, in init_connection_state if self.features.is_sql_auto_is_null_enabled: File "E:\Anaconda\envs\mydjango\lib\site-packages\django\utils\functional.py", line 80, in __get__ res = instance.__dict__[self.name] = self.func(instance) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\mysql\features.py", line 82, in is_sql_auto_is_null_enabled cursor.execute('SELECT @@SQL_AUTO_IS_NULL') File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\utils.py", line 103, in execute sql = self.db.ops.last_executed_query(self.cursor, sql, params) File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_query query = query.decode(errors='replace') AttributeError: 'str' object has no attribute 'decode' ``` 2. : `File "E:\Anaconda\envs\mydjango\lib\site-packages\django\db\backends\mysql\operations.py", line 146, in last_executed_queryquery = query.decode(errors='replace')` decode ,python3 , encode ```python def last_executed_query(self, cursor, sql, params): # With MySQLdb, cursor objects have an (undocumented) "_executed" # attribute where the exact query sent to the database is saved. # See MySQLdb/cursors.py in the source distribution. query = getattr(cursor, '_executed', None) if query is not None: query = query.encode(errors='replace') # decode ,python3 , encode return query ``` 3. 2. sqlite3 + ```python DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), } } ``` 3. () 1. `python manage.py check` : 2. `python manage.py makemigrations app_name` : 3. `python manage.py migrate` : 4. Django | | | | -------------------------------------- | ------------------------------------------------------------ | | `models.CharField(max_length=32)` | , max_length | | `models.IntergerField()` | | | `models.FloatField()` | | | `models.TextField()` | | | `models.EmailField()` | | | `models.DateField()` | () | | `models.DateTimeField()` | () | | `models.FileField(upload_to=file_dir)` | , ,, upload_to | | `models.ImageField(upload_to=img_dir)` | , (),, upload_to | | `` | | + ImageField 1. pillow : `pip install pillow` 2. `setting.py` ```python MEDIA_URL = "/media/" MEDIA_ROOT = os.path.join(BASE_DIR, 'static') ``` 5. Django | | | | -------------- | ---------------------------- | | `verbose_name` | , | | `default` | | | `unique` | | | `blank` | , | | `null` | null | | `max_length` | | | `upload_to` | | | `Auto_now` | | 6. Django ORM 1. Django ORM id 2. Django ORM ,, blank null
Web Proxy Viewer  |  New URL  |  Original Page