Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在django中将值从模型传递到视图? 如何将codeval变量从models.py传递到views.py:_Python_Django - Fatal编程技术网

Python 如何在django中将值从模型传递到视图? 如何将codeval变量从models.py传递到views.py:

Python 如何在django中将值从模型传递到视图? 如何将codeval变量从models.py传递到views.py:,python,django,Python,Django,在models.py中 from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_save import random from django.core.mail import send_mail class UserProfile(models.Model): user = mo

在models.py中

from django.db import models  
from django.contrib.auth.models import User  
from django.db.models.signals import post_save  
import random  
from django.core.mail import send_mail        

class UserProfile(models.Model):  
    user = models.OneToOneField(User)  
    description = models.CharField(max_length=100, default='')  
    city = models.CharField(max_length=100, default='')  
    website = models.URLField(default='')  
    mobile = models.IntegerField(default=0)  
    code = models.IntegerField(default=0)        

def create_profile(sender, **kwargs):  
    if kwargs['created']:  
    user_profile = UserProfile.objects.create(user=kwargs['instance'])  
    codeval = random.randint(111111,999999)    
    user_profile.code = codeval  
    user_profile.save()  
    receiver = User.objects.get(id=user_profile.user_id).email  
    send_mail(' Confirm your account on Abolombon',   
        'Thanks for signing up with Abolombon! Your Abolombon verification code is:\n %s\n\n Thank you\nAbolombon.com'%(codeval),   
        'testpurpose2040@gmail.com',   
        [receiver])

post_save.connect(create_profile, sender=User)  
in views.py

def verification(request, args=codeval):
    return render(request,'website/verification.html',{'args':args})
我正在django制作一个电子商务网站,在那里我需要用户注册选项。为此,我使用django的内置用户注册表单。但我需要存储一些关于用户的附加信息,如联系号码、城市等。我还想验证用户是否向其相应的电子邮件地址发送了6位代码

但在django的内置表单中,它们并没有存储验证代码的字段。因此,我创建了UserProfile模型来实现这一目的。并创建了用于存储6位随机数的代码字段

我想将此随机数存储到代码字段中,并将其发送到用户电子邮件地址。当用户输入代码时,一个函数将用存储的代码验证它。为此,我需要从数据库中检索代码。因此,我想将此键传递到views.py


谢谢

如果我正确解释了您的问题,您需要
UserProfile.objects.get(code=codeval)
。这将为您提供实际的配置文件,因此如果您需要id,只需执行
UserProfile.objects.get(…).id


请注意,您当前的代码可能会为多个配置文件提供相同的代码,因此您应该使用
.filter
而不是
。get

来防止出现这种情况,或者获取所有匹配的配置文件。如果我正确解释了您的问题,您需要
UserProfile.objects.get(code=codeval)
。这将为您提供实际的配置文件,因此如果您需要id,只需执行
UserProfile.objects.get(…).id


请注意,您当前的代码可能会为多个配置文件提供相同的代码,因此您应该防止出现这种情况,或者使用
.filter
而不是
.get

获取匹配的所有配置文件。只需澄清一点,您想从
codeval
获取
用户配置文件
?我需要用户配置文件id来获取UserProfile.objects.get(id=id).code要将其与用户的验证码匹配,请在用户输入验证码的页面上,使用其他什么方法验证其身份?i、 e.是该特定用户的自定义URL,还是他们也输入用户名或电子邮件地址?如果是这样,那么您可以使用该信息查找用户对象和UserProfile对象。def signUpView(请求):If request.method='POST':form=RegistrationForm(request.POST)form.is_active=False form.save(),If form.is_valid():form.save()返回重定向('/website/login/'),否则:form=RegistrationForm()返回render(request,'website/signUp.html',{'form':form})只是为了澄清一下,您想从
codeval
获取
UserProfile
?我需要UerProfile id来获取UserProfile.objects.get(id=id)。在用户输入验证代码的页面上,将其与用户的配置文件匹配,您还使用什么方法来验证他们的身份?i、 e.是该特定用户的自定义URL,还是他们也输入用户名或电子邮件地址?如果是这样,那么您可以使用该信息查找用户对象和UserProfile对象。def signUpView(请求):If request.method='POST':form=RegistrationForm(request.POST)form.is_active=False form.save(),If form.is_valid():form.save()返回重定向('/website/login/'),否则:form=RegistrationForm()返回呈现(请求,'website/signUp.html',{'form':form})