将当前User对象附加到Django表单 [英] Attaching a current User object to Django form

查看:94
本文介绍了将当前User对象附加到Django表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个应用程序,其中包含.txt格式的文件上传表单。我希望当前正在上传文件的用户与文件和文件名一起添加。目前,我可以在管理部分成功完成这项工作,但我无法通过表单本身保存。任何想法?



以下是模型:

  class UploadedTextFile 
file = models.FileField(upload_to =textfiles)
filename = models.CharField(max_length = 50)
username = models.ForeignKey(User,blank = True ,null = True)


class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file','filename ']

这是我的观点:

  def inputtest(request):

#被请求的文件的#response
如果request.method ==POST:
form = UploadedTextFileForm(request.POST)
如果form.is_valid():
new_form = form.save(commit = False)
new_form.username = request.user
new_form.save )


return render(request,'about.html')

inputtest = UploadedTextFileForm()
return render(reque st,'failed.html',{'inputtest':inputtest})

else:
inputtest = UploadedTextFileForm()
return render(request,'inputtest.html' {'inputtest':inputtest})

这是我的html:

  {%extends'base.html'%} 

{%block content%}
< form method = post> {%csrf_token%}
{{inputtest.as_p}}
< input type =submitvalue =Submit/>
< / form>

{%endblock content%}


解决方案

在视图中执行(如您所示)是正确的方法。很可能您遇到问题,因为您已将用户名作为表单上的字段留下,因为FK模型字段没有空白= True 设置表单需要提供字段。您应该在表单的 Meta 类中明确声明要接受用户输入的子集字段。

  class UploadedTextFileForm(ModelForm):
class Meta:
model = UploadedTextFile
fields = ['file','filename']

我不知道为什么在表单无效的时候渲染不同的模板,但无论你不是在上下文中提供表单对象。这意味着您不会看到表单检测到任何错误,这可能是该代码发生了什么 - 您没有看到没有提供 username 的错误。 / p>

I am working on an app that has a section with with a file upload form for .txt fiels. I would like for the current user that is uploading the file to be added along with the file and the file name. Currently, I can do this successfully in the admin section but I just cant get it to save via the form itself. Any Ideas?

Here are the models:

class UploadedTextFile(models.Model):
file = models.FileField(upload_to="textfiles")
filename = models.CharField(max_length = 50)
username = models.ForeignKey(User, blank=True, null=True)


class UploadedTextFileForm(ModelForm):
    class Meta:
        model = UploadedTextFile
        fields = ['file', 'filename']

Here is my view:

def inputtest(request):

#response for file being submited
    if request.method == "POST":
        form = UploadedTextFileForm(request.POST)  
        if form.is_valid():
            new_form = form.save(commit=False)
            new_form.username = request.user
            new_form.save()


        return render(request, 'about.html')

    inputtest = UploadedTextFileForm()
    return render(request, 'failed.html', {'inputtest': inputtest})

    else:
        inputtest = UploadedTextFileForm()
        return render(request, 'inputtest.html', {'inputtest': inputtest})

Here is my html:

{% extends 'base.html' %}

{% block content %}
<form method="post">{% csrf_token %}
{{ inputtest.as_p }}
<input type="submit" value="Submit" />
</form>

{% endblock content %}

解决方案

Doing it in the view (as you've shown) is the right way to do this. Most likely you're having problems because you've left username as a field on the form, and because the FK model field doesn't have blank=True set the form requires the field to be provided. You should explicitly declare just the subset fields that you want to accept user input for in the form's Meta class.

class UploadedTextFileForm(ModelForm):                
    class Meta:                                       
        model = UploadedTextFile
        fields = ['file', 'filename']

I am not sure why you're rendering a different template when the form is not valid, but no matter what you're not providing the form object in the context. This means that you'll never see any errors the form detects, which is probably what's happening with this code - you're not seeing the error that username is not provided.

这篇关于将当前User对象附加到Django表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆