美文网首页python专家之路
django新版本(2.x)踩坑记录

django新版本(2.x)踩坑记录

作者: BlueCat2016 | 来源:发表于2018-09-12 16:28 被阅读0次

新版本urls.py,不再使用正则表达式

提示:

WARNINGS:
?: (2_0.W001) Your URL pattern '^*article/' has a route that contains '(?P<', begins with a '^', or ends with a '$'. This was likely an oversight when migrating to django.urls.path().

之前的写法是:

from django.contrib import admin
from django.urls import path, include
from article import urls

urlpatterns = [
    path('admin/', admin.site.urls),
    path('^*article/', include('article.urls'))
]

新版本的django中的urls.py如果需要用到正则,需要像如下这样写:

from django.contrib import admin
from django.urls import path, include, re_path
from article import urls

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path('^.*article/', include('article.urls'))
]

启动服务之前需要首先运行python manage.py migrate

否则会提示:

You have 14 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.

django2.x报错No module named 'django.core.urlresolvers'

有的资料上写的是from django.core.urlresolvers import reverse
解决办法:

from django.urls import reverse

报错ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported.

提示:

'Specifying a namespace in include() without providing an app_name '
django.core.exceptions.ImproperlyConfigured: Specifying a namespace in include() without providing an app_name is not supported. Set the app_name attribute in the included module, or pass a 2-tuple containing the list of patterns and app_name instead.

解决办法:
在自己的app下的urls中写上appname

from django.contrib import admin
from django.urls import path, re_path

from article import views

app_name = 'article'

urlpatterns = [
    # path('admin/', admin.site.urls),
    path('index/', views.index),
    re_path('test/', views.test, name='article_test')

]

django建表的时候出现sql_mode的警告

运行python manage.py migrate出现如下警告:

WARNINGS:
?: (mysql.W002) MySQL Strict Mode is not set for database connection 'default'
    HINT: MySQL's Strict Mode fixes many data integrity problems in MySQL, such as data truncation upon insertion, by escalating warnings into errors. It is strongly recommended you activate it. See: https://docs.djangoproject.com/en/2.0/ref/databases/#mysql-sql-mode

参考:
https://www.jianshu.com/p/bc41a8bf9d9b

(1054, "Unknown column 'search_top_query.id' in 'field list'")

一开始,models中类是这样设置的:

class SearchTopQuery(models.Model):
    query_word = models.CharField(max_length=500, blank=True, null=True)
    query_counts = models.IntegerField(blank=True, null=True)
    click_counts = models.IntegerField(blank=True, null=True)
    click_rate = models.FloatField(blank=True, null=True)
    item_counts = models.IntegerField(blank=True, null=True)
    log_date = models.CharField(max_length=20, blank=True, null=True)

    class Meta:
        managed = False
        db_table = 'search_top_query'

MySQL数据库表结构:


image.png

解决该问题的方法:
在数据库中操作

ALTER TABLE search_top_query ADD id integer AUTO_INCREMENT NOT NULL PRIMARY KEY FIRST;

TypeError: init() missing 1 required positional argument: 'on_delete'

参考:https://www.cnblogs.com/phyger/p/8035253.html

django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported.

报错:

'provide the namespace argument to include() instead.' % len(arg)
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.

这是由于在django2.0后不支持使用在urls.py中写

url(r'^admin/', include(admin.site.urls))

直接用

path('admin/', admin.site.urls)

django执行python manage.py migrate不生成表

参考:https://blog.csdn.net/hanglinux/article/details/75645756

TypeError: Object of type 'QuerySet' is not JSON serializable

遇到的一个原因是返回的dict中有一个字段是list,该list的元素中有一个字段是datetime类型。
解决方案是将datetime字段转化为string.

RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have A

提交表单报错:RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set.

解决方法:
RuntimeError: You called this URL via POST, but the URL doesn’t end in a slash and you have APPEND_SLASH set.
提示form的action地址最后不是/结尾的,而且APPEND_SLASH的值是Ture

将from的action地址改为/结尾的就可以了
或者
修改settings:APPEND_SLASH=False

Uncaught Error: Bootstrap's JavaScript requires jQuery

解决方案:将jquery.min.js放在bootstrap.min.js文件之前引用,bootstrap.min.css文件在整两个文件前后引用都无妨(测试多次)。

相关文章

网友评论

    本文标题:django新版本(2.x)踩坑记录

    本文链接:https://www.haomeiwen.com/subject/rptbgftx.html