1.
POST提交表单报错:
RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set.
解决办法:
将from的action地址改为 / 结尾的就可以了
urls中的url 要与action 中的url 都带 / 结尾 后者都不带/ 结尾
2.
使用 带正则表达式的 url urls.py 应该这样写 导入包不一样 不用path
from django.conf.urls import url,include
from django.contrib import admin
from app01 import views
urlpatterns = [
url(r'^login/', views.login),
url(r'^index/', views.index),
url(r'^user_info/', views.user_info),
url(r'^userdetail-(?P<nid>\d+)/', views.user_detail),
url(r'^userdel-(?P<nid>\d+)/', views.user_del),
url(r'^useredit-(?P<nid>\d+)/', views.user_edit),
url(r'^orm/', views.orm),
]
3.
使用MySQL连接数据库 在Python3 下 需要在 Django工程下的Django工程名的目录下的__init__.py 中加入:
import pymysql
pymysql.install_as_MySQLdb()
这一原因是:Django 连接MySQL 默认使用的是 MySQLdb模块,但是Python3目前并不支持MySQLdb
4.
model 设置外键需要设置 on_delete 参数设置
CASCADE:这就是默认的选项,级联删除,你无需显性指定它。
PROTECT: 保护模式,如果采用该选项,删除的时候,会抛出ProtectedError错误。
SET_NULL: 置空模式,删除的时候,外键字段被设置为空,前提就是blank=True, null=True,定义该字段的时候,允许为空。
SET_DEFAULT: 置默认值,删除的时候,外键字段设置为默认值,所以定义外键的时候注意加上一个默认值。
SET(): 自定义一个值,该值当然只能是对应的实体了
type=models.ForeignKey(TypeInfo,on_delete='SET_DEFAULT')
5.
报错 render() got an unexpected keyword argument 'renderer' 解决方案
修改报错的函数
注释一行,添加一行
def as_widget(self, widget=None, attrs=None, only_initial=False):
"""
Render the field by rendering the passed widget, adding any HTML
attributes passed as attrs. If a widget isn't specified, use the
field's default widget.
"""
widget = widget or self.field.widget
if self.field.localize:
widget.is_localized = True
attrs = attrs or {}
attrs = self.build_widget_attrs(attrs, widget)
if self.auto_id and 'id' not in widget.attrs:
attrs.setdefault('id', self.html_initial_id if only_initial else self.auto_id)
return widget.render(
name=self.html_initial_name if only_initial else self.html_name,
value=self.value(),
attrs=attrs,
#renderer=self.form.renderer, #注释掉
)
return force_text(html) #添加一行
6.
后台管理语言和时区的设置
#LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'zh-hans'
#TIME_ZONE = 'UTC'
TIME_ZONE = 'Asia/Shanghai'