如何手动呈现Django表单?

时间:2018-09-24 11:39:39

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

是Django中的新功能。有人知道如何执行手册吗? 我有新闻通讯应用程序: newsletter / views.py

from django.shortcuts import render
from .forms import NewsUserForm
from . models import NewsUsers

# Create your views here.

def newsletter_subscribe(request):
    if request.method == 'POST':
        form = NewsUserForm(request.POST)
        if form.is_valid():
           cd = form.cleaned_data
           instance = form.save(commit=False)
           if NewsUsers.objects.filter(email=instance.email).exists():
               print('your email Already exists in our database')

        else:
            instance.save()
            print('your email has been submitted to our database')
    else:
        form = NewsUserForm()
        context = {'form':form}
        template = "home/base.html"
        return render(request, template, context)

新闻通讯/models.py

from django.db import models

# Subscribe models.
class NewsUsers(models.Model):
    name = models.CharField(max_length = 30)
    last_name = models.CharField(max_length = 30)
    email = models.EmailField()
    date_added = models.DateField(auto_now_add=True)

    class Meta:
        verbose_name = "NewsUser"
        verbose_name_plural = "NewsUsers"

        def __str__(self):
                return self.email

新闻通讯/urls.py

from django.urls import path
from .views  import  newsletter_subscribe


app_name = 'newsletter'
urlpatterns = [
    path('subscribe', newsletter_subscribe, name='subscribe'),
 ]

如何从第三个应用程序到家庭模板手动呈现django表单? 我在家用应用程序基本模板中有以下代码: home / templates / base.html

How to use this form on this template?

0 个答案:

没有答案