因为某些不可抗力,被迫开始研究网页搭建。搭建过程中遇到了很多奇葩的错误,列举在此处。
1.用户表拓展问题(AbstractUser方法)
因为是一边研究一边编写数据库,所以在进行用户表拓展之前就在数据库中创建过其他数据表。在执行makemigrations、migrate后并未出现需要的用户表,使用admin的时候出现报错
错误如下:
ProgrammingError at /register/ (1146, "Table 'gitnote.loginapp_user' doesn't exist")
代码如下:
(1)model.py
from django.contrib.auth.models import AbstractUser
class UserInfo(AbstractUser):
sexual = models.CharField(max_length=1)
age = models.IntegerField()
(2)setting.py
AUTH_USER_MODEL = 'app.UserInfo'
【解决方法】
>把数据库全部清空,或者在setting文件中换一个数据库,然后再执行
python3 manage.py makemigrations
python3 manage.py migrate
2.Static无法加载问题
网页的static静态数据无法加载,尝试了很多种办法,最终找到了对我这个项目成立的办法。 先贴一下参考过的链接,可能链接里面的方法对我无效,对你们有效。
https://www.javazxz.com/thread-5414-1-1.html
https://www.easck.com/cos/2020/0619/543366.shtml
https://segmentfault.com/q/1010000004949135/a-1020000005827043
https://blog.51cto.com/853056088/2312332
好了,下面是我的解决方法。
首先修改settings.py的设置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,"templates")],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.media',
'django.template.context_processors.static',
],
},
},
]
STATIC_URL = 'static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
然后修改urls.py的设置
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
然后切换到项目目录,在终端上尝试
python3 manage.py collectstatic
python3 manage.py runserver
解决啦,html里的文件链接写成"/static/…"就好。