Django:具有MultiValueDictKeyError和下载功能的问题

时间:2018-11-28 10:40:42

标签: python django django-forms django-templates django-views

由于两种不同的方式,我想得到您的帮助,以改进我的代码并有可能下载文档。

说明:

第一种方法可以检查某些文档,并且用户必须填写email field。完成后,将根据先前的电子邮件地址显示一个modal并带有预填充字段,因为使用filter上的email adress来执行查询集

这是下载文档的主要过程。

第二种方法不同。我生成一个唯一的超链接,URL中带有文档ID。但是通过这种方式,我不需要以前的电子邮件地址。

我的代码:

这是我的model.py文件:

class Document(models.Model):

    code = models.CharField(max_length=25, verbose_name=_('code'), unique=True, null=False, blank=False)
    language = models.CharField(max_length=2, verbose_name=_('language'), choices=LANGUAGE_CHOICES, null=False, blank=False)
    format = models.CharField(max_length=10, verbose_name=_('format'), choices=FORMAT_CHOICES, null=False, blank=False)
    title = models.CharField(max_length=512, verbose_name=_('title'), null=False, blank=False)
    publication = models.ForeignKey(Publication, verbose_name=_('publication title'), related_name='documents')
    upload = models.FileField(upload_to='media/files/', validators=[validate_file_extension], verbose_name=_('document file'), null=False, blank=False)

    class Meta:
        verbose_name = _('document')
        verbose_name_plural = _('documents')

class Customer(EdqmTable):
    email = models.EmailField(max_length=150, verbose_name=_('e-mail'), null=False)
    first_name = models.CharField(max_length=70, verbose_name=_('first name'), null=False)
    last_name = models.CharField(max_length=70, verbose_name=_('last name'), null=False)
    country = models.ForeignKey(Country, verbose_name=_('country'))
    institution = models.CharField(max_length=255, verbose_name=_('institution'), null=True)

    class Meta:
        verbose_name = _('customer')
        verbose_name_plural = _('customers')

    def __str__(self):
        return f"{self.email}"

这是我的forms.py文件:

class CustomerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = ['email', 'first_name', 'last_name', 'country', 'institution']
        widgets = {
            'email': forms.TextInput(attrs={'placeholder': _('name@example.com')}),
            'first_name': forms.TextInput(attrs={'placeholder': _('First Name')}),
            'last_name': forms.TextInput(attrs={'placeholder': _('Last Name')}),
            'institution': forms.TextInput(attrs={'placeholder': _('Agency, company, academic or other affiliation')}),
        }

    def __init__(self, *args, **kwargs):
        customer_email = kwargs.pop('customer_email', None)

        super(CustomerForm, self).__init__(*args, **kwargs)
        self.fields['country'].empty_label = _('Select a country')
        self.fields['country'].queryset = self.fields['country'].queryset.order_by('name')
        self.fields['email'].required = True
        self.fields['first_name'].required = True
        self.fields['last_name'].required = True
        self.fields['country'].required = True
        self.fields['institution'].required = False

        query_customer_email = Customer.objects.filter(email__iexact=customer_email)

        if customer_email is not None:
            if query_customer_email.exists():
                self.fields['email'].initial = customer_email
                self.fields['first_name'].initial = query_customer_email.distinct()[0].first_name
                self.fields['last_name'].initial = query_customer_email.distinct()[0].last_name
                self.fields['country'].initial = query_customer_email.distinct()[0].country_id
                self.fields['institution'].initial = query_customer_email.distinct()[0].institution
                self.fields['email'].widget.attrs['readonly'] = True
                self.fields['first_name'].widget.attrs['readonly'] = True
                self.fields['last_name'].widget.attrs['readonly'] = True
                self.fields['country'].widget.attrs['readonly'] = True
            else:
                self.fields['email'].initial = customer_email

这是我的模板文件的一部分:

<div class="row">
    <div class=" col-md-5">
        <label for="primary">Email address</label>
        <input type="email" class="form-control" id="email-download-document" name="EmailDownloadDocument"
                 placeholder="Enter email address to get document(s)">
     </div>
 </div>
 <br>
 <div class="row">
     <div class=" col-md-5">
         <input id="document-choice-button" type="submit" class="btn btn-default" name="DocumentSelected"
                 value="{% trans 'Send to my email' %}"/>
     </div>
 </div>

现在,您可以找到我的views.py文件:

#Part according to the first method
class HomeView(CreateView, FormView):
    ...
    def get_form_kwargs(self):
        kwargs = super(HomeView, self).get_form_kwargs()
        if "DocumentSelected" in self.request.GET:
            customer_email = self.request.GET['EmailDownloadDocument']
            kwargs['customer_email'] = customer_email
        return kwargs

# Part according to the second method
class MyRedirectView(RedirectView):

    def get_redirect_url(self, *args, **kwargs):
        q = QueryDict(mutable=True)
        q['DocumentChoice'] = self.kwargs['code']
        q['DocumentSelected'] = 'Send to my email'
        my_site = settings.MY_SITE_URL

        if my_site == 'http://localhost:8000':
            return f"{my_site}/app/home?{q.urlencode()}"
        else:
            return f"{my_site}/dev3/app/app/home?{q.urlencode()}"

最后是我的urls.py文件:

urlpatterns = [
    url(r'^app/home$', HomeView.as_view(), name='app-home'),
    ...
    url(r'^app/direct/download/(?P<code>[\w\.-]+)/$',MyRedirectView.as_view(), name='go-to-direct-download'),

问题:

第一种方法效果很好!我在这部分没有任何问题。

但是第二个命令无法按我的意愿工作,因为我不知道如何避免使用电子邮件。因为这是直接链接,所以我必须显示模式,但必须使用空表格,因为所有人都可以通过此链接进行访问。因此,我不必获取预加载数据。

示例:

第一种方法:

[![在此处输入图片描述] [1]] [1]

第二种方法:

使用这种方法,我的网址例如:

http://localhost:8000/app/direct/download/PUBSD-0001-FR-PDF-1714

它变成了:

http://localhost:8000/app/home?DocumentChoice=PUBSD-0001-FR-PDF-1714&DocumentSelected=Send+to+my+email

但是我遇到了这个问题:

  

异常类型:/ app / home处的MultiValueDictKeyError异常   值:“'EmailDownloadDocument'”

我不想在此方法中使用电子邮件部分。

非常感谢您!

编辑:

我发现了一些东西:

我这样修改了get_form_kwargs()方法:

def get_form_kwargs(self):
    kwargs = super(HomeView, self).get_form_kwargs()
    if "DocumentSelected" in self.request.GET:
        customer_email = self.request.GET.get('EmailDownloadDocument', '')
        kwargs['customer_email'] = customer_email
    return kwargs

现在似乎可以使用,对吗?

0 个答案:

没有答案