如何将Django模型和表单与我的视图

时间:2017-08-15 17:16:00

标签: python html django django-models django-forms

如何将我的html集成到我的表单和模型中,以便它可以处理后端。在管理页面中,一切正常,但我无法将其与我的前端集成。 这是我的models.py

from django.db import models

class SignUp(models.Model):
    name = models.CharField(max_length=120, blank=True, null=True)
    email = models.EmailField()
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

    def __unicode__(self):
        return self.email

这是我的forms.py

from django import forms
from .models import SignUp

class SignUpForm(forms.ModelForm):
    class Meta:
        model = SignUp
        fields = ['name', 'email']

    def clean_name(self):
        name = self.cleaned_data.get('name')

        return name 

    def clean_email(self):
        email = self.cleaned_data.get('email')

        try:
            match = SignUp.objects.get(email=email)
        except SignUp.DoesNotExist:
            return email

        raise forms.ValidationError('This email address is already subscribed.')

这是我的view.py

from django.shortcuts import render
from django.core.mail import send_mail
from django.conf import settings 

from .forms import SignUpForm

def signUp(request):
    form = SignUpForm(request.POST or None)

    if form.is_valid():
        name = form.clean_name()
        email = form.clean_email()
        instance = form.save()

        subject = 'Bruke Church news'
        from_email = settings.EMAIL_HOST_USER
        to_email = [email]
        contact_message = "%s:Thank you for signing up for our newsletter via %s. we'll be in touch" %( 
                                        name,
                                         email)

        send_mail (subject, 
                            contact_message, 
                            from_email, 
                            to_email,
                            fail_silently=False)


    context = {
        "form": form
    }


    return render(request, "signUp.html",context)

这是我的html文件

<div class="col-md-5">
              <div class="row">
                <div class="col-md-7">
                  <font color="#4C194C">Newsletter Sign Up</font>
                  <form action=" " method="POST">
                  {% csrf_token %}
                  <div class="form-group">
                    <input class="form-control" id="name" type="text" placeholder="Your Name *" name="name">
                  </div>
                  <div class="form-group">
                    <input class="form-control" id="email" type="email" placeholder="Your Email *" name="email">
                  </div>
                    <button class="btn btn-primary btn-sm" type="submit">Submit</button>
                  </form>
                </div>
              </div>
          </div>

感谢您的时间和精力。 N.B我不想在我的html中使用{{form.as_p}}方法

1 个答案:

答案 0 :(得分:0)

说实话我认为您需要改进代码(例如模型和表单)。 为什么需要保持SignUp模型?这是另一张将保存在您的数据库中的表,您真的需要它吗? 在您的表单中,您只需使用SignUp.objects.exists(email=email)检查电子邮件是否存在。

    def clean_email(self):
       email = self.cleaned_data.get('email')
       match = SignUp.objects.exists(email=email)
       if not match:
           raise forms.ValidationError('This email address is already 
                                        subscribed.')
       return email

最后,在验证您的请求后,您只需使用参数键访问request.data即可。