『壹』 如何正確使用 Django Admin
1. Django Admin 不適合最終用戶使用
Django admin是為管理員設計的, 而不是給最終用戶使用的. Django admin的存在滑啟是為了方便管理員添加修改刪除數據和管理站點任務.
2. 列表顯示
如上圖, 因為model的默認文本顯示是 xxx object, 所以在admin列表頁中默認顯示的也是xxx object. 如果希望顯示更有用的信息 我們可以進行如下設置:
為model寫__unicode__()方法
如果希望model在列表頁中顯示乎枝更多項, 則需要使用list_display
__unicode()__例子:
# models.py
from django.db import models
class Article(models.Model):
title = models.CharField(max_length=100)
slug = models.CharField(max_length=100)
content = models.TextField()
is_published = models.BooleanField(default=False)
def __unicode__(self):
return self.title
# admin.py
from django.contrib import admin
from .models import Article
admin.site.register(Article)
結果:
如果希望顯示其歲讓敏他項:
# admin.py
from django.contrib import admin
from .models import Article
class ArticleAdmin(admin.ModelAdmin):
list_display = ('title', 'is_published')
admin.site.register(Article, ArticleAdmin)
『貳』 為什麼將django admin 默認資料庫結構傳到bae上會出錯
天然手凱碧支持呀。 一般出現無法孫滑插入都是設置問題,比如資料庫畢舉page code沒配好, django 的settings.py裡面某些配置不對。
『叄』 在執行django-admin時,總是拋出錯誤MoleNotFoundError: No mole named 'mysite'怎麼辦
這是由包重名導致的問題,沒有配置對路徑,解釋稿陸器沒有找到你的mysite模塊。
卸載後重新安裝django1.3.7 版本即可。
『肆』 python3.6下如何用Django1.9創建資料庫報錯
錯誤描述
python==3.5
django==1.7
django創建項目時報錯如下:
Traceback (most recent call last):
File "/root/envs/django-test/bin/django-admin", line 11, in <mole>
sys.exit(execute_from_command_line())
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/root/envs/django-test/lib/python3.5/site-packages/旅弊django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/log.py", line 10, in <mole>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/root/envs/django-test/lib/python3.5/site-packages/django/views/debug.py", line 10, in <mole>
from django.http import (HttpResponse, HttpResponseServerError,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/__init__.py", line 4, in <mole>
from django.http.response import (HttpResponse, StreamingHttpResponse,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/response.py", line 13, in <mole>
from django.core.serializers.json import DjangoJSONEncoder
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in <mole>
from django.core.serializers.base import SerializerDoesNotExist
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in <mole>
from django.db import models
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in <mole>
from django.db.models.query import Q, QuerySet, Prefetch # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/query.py", line 13, in <姿冊mole>
from django.db.models.fields import AutoField, Empty
File "跡鎮宏/root/envs/django-test/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in <mole>
from django import forms
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in <mole>
from django.forms.fields import * # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/fields.py", line 18, in <mole>
from django.forms.utils import from_current_timezone, to_current_timezone
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/utils.py", line 15, in <mole>
from django.utils.html import format_html, format_html_join, escape
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html.py", line 16, in <mole>
from .html_parser import HTMLParser, HTMLParseError
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <mole>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: mole 'html.parser' has no attribute 'HTMLParseError'
(django-test) root@localhost:~/source/djangotest# django-admin startproject superlists
Traceback (most recent call last):
File "/root/envs/django-test/bin/django-admin", line 11, in <mole>
sys.exit(execute_from_command_line())
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
utility.execute()
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/management/__init__.py", line 354, in execute
django.setup()
File "/root/envs/django-test/lib/python3.5/site-packages/django/__init__.py", line 18, in setup
from django.utils.log import configure_logging
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/log.py", line 10, in <mole>
from django.views.debug import ExceptionReporter, get_exception_reporter_filter
File "/root/envs/django-test/lib/python3.5/site-packages/django/views/debug.py", line 10, in <mole>
from django.http import (HttpResponse, HttpResponseServerError,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/__init__.py", line 4, in <mole>
from django.http.response import (HttpResponse, StreamingHttpResponse,
File "/root/envs/django-test/lib/python3.5/site-packages/django/http/response.py", line 13, in <mole>
from django.core.serializers.json import DjangoJSONEncoder
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/__init__.py", line 23, in <mole>
from django.core.serializers.base import SerializerDoesNotExist
File "/root/envs/django-test/lib/python3.5/site-packages/django/core/serializers/base.py", line 6, in <mole>
from django.db import models
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/__init__.py", line 6, in <mole>
from django.db.models.query import Q, QuerySet, Prefetch # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/query.py", line 13, in <mole>
from django.db.models.fields import AutoField, Empty
File "/root/envs/django-test/lib/python3.5/site-packages/django/db/models/fields/__init__.py", line 18, in <mole>
from django import forms
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/__init__.py", line 6, in <mole>
from django.forms.fields import * # NOQA
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/fields.py", line 18, in <mole>
from django.forms.utils import from_current_timezone, to_current_timezone
File "/root/envs/django-test/lib/python3.5/site-packages/django/forms/utils.py", line 15, in <mole>
from django.utils.html import format_html, format_html_join, escape
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html.py", line 16, in <mole>
from .html_parser import HTMLParser, HTMLParseError
File "/root/envs/django-test/lib/python3.5/site-packages/django/utils/html_parser.py", line 12, in <mole>
HTMLParseError = _html_parser.HTMLParseError
AttributeError: mole 'html.parser' has no attribute 'HTMLParseError'5767757677
原因
HTMLParseError在pythons3.5已經沒有了
解決方法
將python版本回退到3.3或3.4
升級django版本
『伍』 Django 是不是不用寫後台的,用自帶的 admin 管理資料庫就行了
Django是個框架,後台是固定的,只可以小范圍修改,差不多算是不用些後台
『陸』 django 怎麼在admin里顯示資料庫的內容
操作方法:首先要運行python manage.py createsuperuser 命令來創建一個管理員賬戶。
然後在url中輸入/admin 即可到達管理員登錄頁,登錄之後會發現並沒有資料庫中要顯示的項目,因為我們還沒有注冊。
接下來我們注冊要在admin中管理的數據模型;在admin.py中注冊模型。然後刷新頁面,即可看到ContactMessage這個數據表了,可以在里邊增刪進行簡單的增刪改查。
『柒』 如何獨立使用django的資料庫訪問功能
1. 安裝Django
[plain] view plain
cd Django-1.4
python setup.py install
2. 安裝postgresql的客戶端:
[plain] view plain
sudo apt-get install -y postgresql-client-9.1 python-psycopg2
3. 新建project:
[plain] view plain
django-admin.py startproject myproject
4. 在myproject下新建app:
[plain] view plain
python manage.py startapp myapp
4. 新增環境變數:
編輯/etc/profile文件,在末尾加入以下語句:
[plain] view plain
<span style="color:#FF0000;"> export PYTHONPATH=$PYTHONPATH:/home/yc/src/myproject
export DJANGO_SETTINGS_MODULE=myproject.settings</span>
5.
假設資料庫已經由Django的另一個應用(名稱為otherapp)建好,資料庫類型是postgresql,名稱為mydb,位於
192.168.1.23。見好的資料庫中有一個表,名稱是otherapp_user,則將otherapp/models.py拷貝到myapp
/models.py。
注意檢查models.py下的class user類的class Meta:部分,如果沒有applabel標簽,則要加上:app_label='otherapp'
再修改/home/yc/src/myproject下的settings.py文件,如下:
[plain] view plain
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'mydb', # 'vps2db_test1' Or path to database file if using sqlite3.
'USER': 'postgres', # Not used with sqlite3.
'PASSWORD': '123', # Not used with sqlite3.
'HOST': '192.168.1.23', # Set to empty string for localhost. Not used with sqlite3.
'PORT': '5432', # Set to empty string for default. Not used with sqlite3.
}
}
[plain] view plain
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'<span style="color:#FF0000;">myapp</span>',
)
6. 在myapp目錄下編寫測試程序
[python] view plain
from models import user
if __name__=="__main__":
try:
u = user.objects.get(id=user_id)
except user.DoesNotExist:
print "user not exist)
return None
else:
return u
『捌』 Django如何更新資料庫
最簡單的辦法是。為資料庫的表建立一個model。具體做法是這樣子。
1.在settings.py里設置資料庫連接方式。連接錯誤後面都沒有辦法
2.在models設置一個資料庫表的對應數據結構,通常叫關系對象模型,所以叫model,它就是一個類。你可以用django-admin.py ...probe,似乎是這個命令,就是一個資料庫的探測命令,可以根據表,自動生成model的代碼。當然如果你先寫了model的代碼就可以用syncdb命令生成資料庫的表
3.在urls.py里設置一個URL的path,當瀏覽器訪問這個path時就可以打開對應的view中的函數
4.在view里
4.1 result=modelclass.objects.all()這個命令可以取出所有的記錄
4.2 然後你可以直接將結果傳遞給模板。由模板顯示。這是最合理的辦法
4.3 在模板里 {% for cols in result %}
4.4 <li>cols.field1</li>
大約就是這樣子。可能說的太粗。你對著教程看就知道了。