我一直在尝试用XP字段扩展用户模型。在编写了代码(我认为它是正确的)之后,我决定尝试一下。你知道吗
不幸的是,我得到了这个错误:
ProgrammingError at /register/
relation "accounts_userprofile" does not exist
LINE 1: INSERT INTO "accounts_userprofile" ("user_id", "xp") VALUES ...
^
我认为这是因为我扩展了用户模型,但没有运行迁移。 这是我的密码: 视图.py你知道吗
from django.shortcuts import render, redirect
from django.contrib import messages
from django.contrib.auth.models import User
from django.contrib import auth
from .models import UserProfile
# Create your views here.
def login(req):
if req.method == 'POST':
username = req.POST['username']
password = req.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(req, user)
messages.success(req, 'Acum eşti logat')
return redirect('dashboard')
else:
messages.error(req, 'Numele de utilizator sau parola sunt greşite')
return redirect('login')
return
else:
return render(req, "../templates/pagini/login.html")
def register(req):
if req.method == 'POST':
first_name = req.POST['first_name']
last_name = req.POST['last_name']
username = req.POST['username']
email = req.POST['email']
password = req.POST['password']
password2 = req.POST['password2']
if password == password2:
if User.objects.filter(username=username).exists():
messages.error(req, 'Numele de utilizator deja există')
return redirect('register')
else:
if User.objects.filter(email=email).exists():
messages.error(req, 'E-mailul este deja folosit')
return redirect('register')
else:
user = User.objects.create_user(username=username, password=password, email=email, first_name=first_name, last_name=last_name)
user.UserProfile.xp = 0
user.save()
messages.success(req, 'Acum eşti logat')
return redirect('login')
else:
messages.error(req, 'Parolele diferă')
return redirect('register')
else:
return render(req, "../templates/pagini/register.html")
def dashboard(req):
return render(req, "../templates/pagini/dashboard.html")
你知道吗型号.py你知道吗
from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
xp = models.IntegerField(default=0)
@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
if created:
UserProfile.objects.create(user=instance)
@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
instance.profile.save()
当我运行python时管理.pyshowmigrations我看不到此应用程序的任何迁移-这是我以“帐户”的名称创建的应用程序。你知道吗
我想我遗漏了一些东西,因为当我有另一个模型的时候,我做了一个迁移,但它没有像预期的那样工作。现在我看不到此应用的任何迁移,即使我从中删除了migrations文件夹。你知道吗
那么,错误是否来自未运行“migrate”?如果是,我应该如何运行它,因为我看不到它的任何迁移。你知道吗
谢谢。你知道吗
你需要做什么
python manage.py makemigrations <myapp>
其中是应用程序的名称, 这将使django检测到对模型的更改python manage.py migrate
将更改提交到数据库相关问题 更多 >
编程相关推荐