[英]Unique constraint failed - Integrity error - django
Can't find a solution for my probelm online.无法在线找到我的问题的解决方案。 error message I am receiving regarding problem ----- IntegrityError at /add_user_address/ UNIQUE constraint failed: users_useraddress.user_id ------ all help greatly appreciated!
我收到有关问题的错误消息 ----- /add_user_address/ UNIQUE 约束处的 IntegrityError 失败:users_useraddress.user_id ------ 非常感谢所有帮助!
views.py import re views.py重新导入
from django.urls import reverse
from django.shortcuts import render, redirect, Http404, HttpResponseRedirect
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from .forms import UserRegisterForm, UserAddressForm
from .models import EmailConfirmed
def register(request):
if request.method == 'POST':
form = UserRegisterForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
messages.success(request, f'Your registration has been successful! Please check your email to confirm account.')
return redirect('login')
else:
form = UserRegisterForm()
return render(request, 'users/register.html', {'form': form})
SHA1_RE = re.compile('^[a-f0-9]{40}$')
def activation_view(request, activation_key):
if SHA1_RE.search(activation_key):
print("activation key is real")
try:
instance = EmailConfirmed.objects.get(activation_key=activation_key)
except EmailConfirmed.DoesNotExist:
instance = None
messages.success(request, f'There was an error with your request.')
return HttpResponseRedirect("/")
if instance is not None and not instance.confirmed:
page_message = "Confirmation successful, welcome to Fuisce! "
instance.confirmed = True
instance.activation_key = "Confirmed"
instance.save()
elif instance is not None and instance.confirmed:
page_message = "Already Confirmed"
else:
page_message = ""
context = {"page_message": page_message}
return render(request, "users/activation_complete.html", context)
else:
raise Http404
@login_required
def profile(request):
return render(request, 'users/profile.html')
**def add_user_address(request):
print(request.GET)
try:
redirect = request.GET.get("redirect")
except:
redirect = None
if request.method == "POST":
form = UserAddressForm(request.POST)
if form.is_valid():
new_address = form.save(commit=False)
new_address.user = request.user
new_address.save()
if redirect is not None:
return HttpResponseRedirect(reverse(str(redirect)))
else:
raise Http404**
html file html文件
{% extends "fuisce/base.html" %}
{% block content %}
<form method="POST" action='{% url "add_user_address" %}?redirect=checkout'>{% csrf_token %}
{{ address_form.as_p }}
<input type='submit' class='btn btn-primary' value='Add Address'/>
</form>
{% endblock content %}
My html and views file are attached, let me know if anything else is needed to solve the problem.附上我的 html 和视图文件,如果需要其他任何东西来解决问题,请告诉我。
from django.db import models
from django.core.mail import send_mail
from django.contrib.auth.models import User
from django.conf import settings
from django.template.loader import render_to_string
from django.urls import reverse
from localflavor.ie.ie_counties import IE_COUNTY_CHOICES
class UserAddress(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
address = models.CharField(max_length=150 )
address2 = models.CharField(max_length=150, null=True, blank=True)
city = models.CharField(max_length=100, null=True, blank=True)
county = models.CharField(max_length=100, choices=IE_COUNTY_CHOICES)
country = models.CharField(max_length=100)
postalcode = models.CharField(max_length=30)
phone = models.CharField(max_length=60)
shipping = models.BooleanField(default=True)
billing = models.BooleanField(default=True)
timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
updated = models.DateTimeField(auto_now_add=False, auto_now=True)
def __str__(self):
return str(self.user.username)
Add your models file - unique constrain is connected with your model for database.添加您的模型文件 - 唯一约束与您的 model 数据库连接。 Probably you add for one field in your model attribute "unique=True".
可能您在 model 属性“unique=True”中添加一个字段。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.