当前位置:首页 » 数据仓库 » djangoadmin数据库
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

djangoadmin数据库

发布时间: 2023-03-19 01:18:06

‘壹’ 如何正确使用 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>
大约就是这样子。可能说的太粗。你对着教程看就知道了。