Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.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 Auth”;“用户”;提交表格时的身份证_Python_Django_Primary Key_Django Forms - Fatal编程技术网

Python “获取Django Auth”;“用户”;提交表格时的身份证

Python “获取Django Auth”;“用户”;提交表格时的身份证,python,django,primary-key,django-forms,Python,Django,Primary Key,Django Forms,我目前有一个模型表单,它将输入的域提交给db 我遇到的问题是,当提交域以满足db端的PK-FK关系时,我需要保存当前登录的用户ID(django.auth表中的PK) 我目前有: class SubmitDomain(ModelForm): domainNm = forms.CharField(initial=u'Enter your domain', label='') FKtoClient = User.<something> class Meta:

我目前有一个模型表单,它将输入的域提交给db

我遇到的问题是,当提交域以满足db端的PK-FK关系时,我需要保存当前登录的用户ID(django.auth表中的PK)

我目前有:

class SubmitDomain(ModelForm):
    domainNm = forms.CharField(initial=u'Enter your domain', label='')
    FKtoClient = User.<something>

    class Meta:
        model = Tld #Create form based off Model for Tld
        fields = ['domainNm']

def clean_domainNm(self):
    cleanedDomainName = self.cleaned_data.get('domainNm')
    if Tld.objects.filter(domainNm=cleanedDomainName).exists():
        errorMsg = u"Sorry that domain is not available."
        raise ValidationError(errorMsg)
    else:
        return cleanedDomainName
问题是它给了我一个错误:(1048,“列'FKtoClient_id'不能为null”),非常奇怪的事情发生了,对于列
FKtoClient
,它试图提交:
7L
,而不是
7
(此用户记录的主键)。有什么想法吗?


如果有人能提供帮助,我将非常感激

您可以从请求对象获取登录用户:

current_user = request.user

您可以从请求对象获取登录用户:

current_user = request.user

您可以从请求对象获取登录用户:

current_user = request.user

您可以从请求对象获取登录用户:

current_user = request.user

如果要要求用户登录以提交表单,可以执行以下操作:

@login_required # if a user iS REQUIRED to be logged in to save a form
def your_view(request):
   form = SubmitDomain(request.POST)
   if form.is_valid():
     new_submit = form.save(commit=False)
     new_submit.your_user_field = request.user
     new_submit.save()

如果要要求用户登录以提交表单,可以执行以下操作:

@login_required # if a user iS REQUIRED to be logged in to save a form
def your_view(request):
   form = SubmitDomain(request.POST)
   if form.is_valid():
     new_submit = form.save(commit=False)
     new_submit.your_user_field = request.user
     new_submit.save()

如果要要求用户登录以提交表单,可以执行以下操作:

@login_required # if a user iS REQUIRED to be logged in to save a form
def your_view(request):
   form = SubmitDomain(request.POST)
   if form.is_valid():
     new_submit = form.save(commit=False)
     new_submit.your_user_field = request.user
     new_submit.save()

如果要要求用户登录以提交表单,可以执行以下操作:

@login_required # if a user iS REQUIRED to be logged in to save a form
def your_view(request):
   form = SubmitDomain(request.POST)
   if form.is_valid():
     new_submit = form.save(commit=False)
     new_submit.your_user_field = request.user
     new_submit.save()

首先,从表单中删除
FKtoClient
。您需要在视图中设置用户,以便对请求对象执行yes操作。无法在窗体上设置自动设置当前用户的属性

实例化表单时,可以传递一个已设置了用户的
tld
实例

def AccountHome(request):
    # I recommend using the login required decorator instead but this is ok
    if request.user.is_anonymous():
        return HttpResponseRedirect('/Login/')

    # create a tld instance for the form, with the user set
    tld = Tld(FKtoClient=request.user)
    form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance

    if request.method == 'POST': # If the form has been submitted...
        if form.is_valid(): # If form input passes initial validation...
            domainNm = form.cleaned_data['domainNm']
            form.save() #save cleaned data to the db from dictionary

            # don't use a try..except block here, it shouldn't raise an exception
            return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
    # No need to create another form here, because you are using the request.POST or None trick
    # else:
    #    form = SubmitDomain()

    tld_set = request.user.tld_set.all()

    return render(request, 'VA/account/accounthome.html', {
         'tld_set':tld_set, 'form' : form
    })

这比@dm03514的答案有一个优势,即如果需要,您可以在表单方法中作为
self.instance.user
访问
user

首先,从表单中删除
FKtoClient
。您需要在视图中设置用户,以便对请求对象执行yes操作。无法在窗体上设置自动设置当前用户的属性

实例化表单时,可以传递一个已设置了用户的
tld
实例

def AccountHome(request):
    # I recommend using the login required decorator instead but this is ok
    if request.user.is_anonymous():
        return HttpResponseRedirect('/Login/')

    # create a tld instance for the form, with the user set
    tld = Tld(FKtoClient=request.user)
    form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance

    if request.method == 'POST': # If the form has been submitted...
        if form.is_valid(): # If form input passes initial validation...
            domainNm = form.cleaned_data['domainNm']
            form.save() #save cleaned data to the db from dictionary

            # don't use a try..except block here, it shouldn't raise an exception
            return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
    # No need to create another form here, because you are using the request.POST or None trick
    # else:
    #    form = SubmitDomain()

    tld_set = request.user.tld_set.all()

    return render(request, 'VA/account/accounthome.html', {
         'tld_set':tld_set, 'form' : form
    })

这比@dm03514的答案有一个优势,即如果需要,您可以在表单方法中作为
self.instance.user
访问
user

首先,从表单中删除
FKtoClient
。您需要在视图中设置用户,以便对请求对象执行yes操作。无法在窗体上设置自动设置当前用户的属性

实例化表单时,可以传递一个已设置了用户的
tld
实例

def AccountHome(request):
    # I recommend using the login required decorator instead but this is ok
    if request.user.is_anonymous():
        return HttpResponseRedirect('/Login/')

    # create a tld instance for the form, with the user set
    tld = Tld(FKtoClient=request.user)
    form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance

    if request.method == 'POST': # If the form has been submitted...
        if form.is_valid(): # If form input passes initial validation...
            domainNm = form.cleaned_data['domainNm']
            form.save() #save cleaned data to the db from dictionary

            # don't use a try..except block here, it shouldn't raise an exception
            return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
    # No need to create another form here, because you are using the request.POST or None trick
    # else:
    #    form = SubmitDomain()

    tld_set = request.user.tld_set.all()

    return render(request, 'VA/account/accounthome.html', {
         'tld_set':tld_set, 'form' : form
    })

这比@dm03514的答案有一个优势,即如果需要,您可以在表单方法中作为
self.instance.user
访问
user

首先,从表单中删除
FKtoClient
。您需要在视图中设置用户,以便对请求对象执行yes操作。无法在窗体上设置自动设置当前用户的属性

实例化表单时,可以传递一个已设置了用户的
tld
实例

def AccountHome(request):
    # I recommend using the login required decorator instead but this is ok
    if request.user.is_anonymous():
        return HttpResponseRedirect('/Login/')

    # create a tld instance for the form, with the user set
    tld = Tld(FKtoClient=request.user)
    form = SubmitDomain(data=request.POST or None, instance=tld) # A form bound to the POST data, using the tld instance

    if request.method == 'POST': # If the form has been submitted...
        if form.is_valid(): # If form input passes initial validation...
            domainNm = form.cleaned_data['domainNm']
            form.save() #save cleaned data to the db from dictionary

            # don't use a try..except block here, it shouldn't raise an exception
            return HttpResponseRedirect('/Processscan/?domainNm=%s' % domainNm)
    # No need to create another form here, because you are using the request.POST or None trick
    # else:
    #    form = SubmitDomain()

    tld_set = request.user.tld_set.all()

    return render(request, 'VA/account/accounthome.html', {
         'tld_set':tld_set, 'form' : form
    })

这比@dm03514的答案有一个优势,即如果需要,您可以在表单方法中以
self.instance.user
的形式访问
user

但是,如何从django auth user表中获取用户ID?该用户已通过正确身份验证。是否可以在forms.py中使用?它需要哪些导入?但是,如何从django auth user表中获取用户ID?该用户已通过正确身份验证。是否可以在forms.py中使用?它需要哪些导入?但是,如何从django auth user表中获取用户ID?该用户已通过正确身份验证。是否可以在forms.py中使用?它需要哪些导入?但是,如何从django auth user表中获取用户ID?该用户已通过正确身份验证。是否可以在forms.py中使用?它需要哪些导入?这不是请求。用户已通过身份验证。他/她正在提交表单,提交时表单当前会提交表单条目,但还必须包含已登录用户的id。这有意义吗?此示例视图显示了在保存表单时如何设置用户。您应该能够调整视图以执行相同的操作。如果您试图在Django管理员而不是视图中执行此操作,您应该更新您的问题,这样说。这不是请求。用户已通过身份验证。他/她正在提交表单,提交时表单当前会提交表单条目,但还必须包含已登录用户的id。这有意义吗?此示例视图显示了在保存表单时如何设置用户。您应该能够调整视图以执行相同的操作。如果您试图在Django管理员而不是视图中执行此操作,您应该更新您的问题,这样说。这不是请求。用户已通过身份验证。他/她正在提交表单,提交时表单当前会提交表单条目,但还必须包含已登录用户的id。这有意义吗?此示例视图显示了在保存表单时如何设置用户。您应该能够调整视图以执行相同的操作。如果您试图在Django管理员而不是视图中执行此操作,您应该更新您的问题,这样说。这不是请求。用户已通过身份验证。他/她正在提交表单,提交时表单当前会提交表单条目,但还必须包含已登录用户的id。这有意义吗?此示例视图显示了在保存表单时如何设置用户。您应该能够调整视图以执行相同的操作。如果您试图在Django管理员而不是视图中执行此操作,则应更新您的问题以说明这一点。