有什么需要使用<meta>将CSRF令牌名称和值放在<head>标记内:
例如:
<meta content="authenticity_token" name="csrf-param" />
<meta content="4sWPhTlJAmt1IcyNq1FCyivsAVhHqjiDCKRXOgOQock=" name="csrf-token" />
Run Code Online (Sandbox Code Playgroud)
我已经阅读了关于将CSRF值保留在cookie中的概念,但没有找到保留在<head>标记内的原因.
运行时出现此错误python manage.py migrate:
ValueError:字段account.UserProfile.user:auth.User引用的模型的查找失败
我做的步骤:
1.创建项目并添加新应用程序:
$ django-admin.py startproject djdev
$ cd djdev
$ python manage.py startapp account
Run Code Online (Sandbox Code Playgroud)
2.我增加了新的应用INSTALLED_APPS在djdev/settings.py:
...
'django.contrib.staticfiles',
'account',
)
...
Run Code Online (Sandbox Code Playgroud)
3.在以下位置创建了一个新的UserProfile模型类account/models.py:
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
"""
User Profile having one-to-one relations with User
"""
class Meta:
db_table = 'user_profile'
ordering = ['id']
user = models.OneToOneField(User, db_column='id_user', related_name='profile')
mobile_no = models.CharField('Mobile no.', db_column='contact_no_home', max_length=16, blank=True, null=True)
address_line_1 = …Run Code Online (Sandbox Code Playgroud) 我google了很多次,发现只有一个解决方案,通过第三个模型为两个模型添加自定义中间模型.我像往常一样应用了建议,但仍然遇到这个问题:
不能包含ManyToManyField字段'terms',因为'terms'手动指定'through'模型
class Term(models.Model):
class Meta:
db_table = "tbl_term"
name = models.CharField(max_length=32)
class Post(models.Model):
class Meta:
db_table = "tbl_post"
title = models.CharField(max_length=96)
content = models.TextField()
terms = models.ManyToManyField("Term", through="TermRelation")
class TermRelation(models.Model):
class Meta:
db_table = "tbl_term_relation"
term = models.ForeignKey("Term", db_column="id_term")
post = models.ForeignKey("Post", db_column="id_post")
Run Code Online (Sandbox Code Playgroud)
@admin.register(Term)
class AdminTerm(admin.ModelAdmin):
fieldsets = [
(None, {'fields': ['name']})
]
@admin.register(Post)
class AdminPost(admin.ModelAdmin):
fieldsets = [
(None, {'fields':['title', 'content', 'terms']})
]
Run Code Online (Sandbox Code Playgroud) 使用下面的脚本计算执行时间:
$ php -r "\$tt=microtime();for(\$i=0;\$i<111120;\$i++) \
echo hash('crc32', microtime()).PHP_EOL;echo 'Time took: ' \
. (microtime()-\$tt).PHP_EOL;"
Run Code Online (Sandbox Code Playgroud)
结果(多个):
... Time took: 0.266269 ... Time took: -0.725037 ... Time took: 0.264577 ... Time took: 0.655573 ... Time took: -0.389367 ... Time took: -0.451503 ... Time took: 0.50867
为什么时间计算有时会返回负值?
使用现有数据库.所有现有表都以前缀为命名空间tbl_.如何tbl_在查询或同步之前创建模型类或将django设置配置为前缀.
此外,每个子表在字段上都有外键,每个字段都以名称空间为前缀id_[table_name].我也想配置外键.