Django send_mail应用程序 - 将其与Yeoman前端挂钩 [英] Django send_mail application - hook it up with Yeoman frontend

查看:112
本文介绍了Django send_mail应用程序 - 将其与Yeoman前端挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Django作为后端,Yeoman作为前端。我是新来的我的前端本地服务器在 localhost:9000 上运行,后端服务器在 localhost:8000 上运行。



我刚刚在Django教程之后构建了一个电子邮件发件人应用程序。它的工作完美,包括:



表单:

 类ContactForm(forms.Form):
name = forms.CharField()
email = forms.EmailField()
telephoneNr = forms.IntegerField()
message = forms.CharField (widget = forms.Textarea)

视图:


$ b $从django.core.mail导入send_mail
从django.shortcuts导入render,render_to_response
从django.http导入HttpResponseRedirect,HttpResponse
从django.core.mail导入b $ $ $ $ $ mailsender.forms import ContactForm

def contact(request):
if request.method =='POST':
form = ContactForm(request.POST)
if form.is_valid():
success = True
name = form.cleaned_data ['name']
email = form.cleaned_data ['email']
phoneNr = form.cleaned_data ['tlf']
message = form.cleaned_data ['melding']
receiverEmail = ['somewhere@example.com']
text = message +'\\\
'+ name +'\\\
'+ str(phoneNr)

send_mail('Contact form ',beskjed,email,receiverEmail)
return render(request,result.html)

else:
form = ContactForm(
return render 'contactform.html',{'form':form})

我的HTML:

 < h1>联系我们< / h1> 

{%if form.errors%}
< p style =color:red;>
请更正以下错误{{form.errors | pluralize}}。
< / p>
{%endif%}

< form action =method =post>
< table>
{{form.as_p}}
< / table>
{%csrf_token%}
< input type =submitvalue =Submit>
< / form>

如果我访问 localhost:8000 / contactform ,联系表单正如我想要的那样显示,它会发送电子邮件。



我需要帮助找出如何将这个视图连接到Yeoman前端 - 作为搜索互联网(和SO)导致我陷入混乱的道路。我应该使用 Tastypie 吗?我真的想把这个逻辑保留在后端。任何帮助我指出正确的方向是非常感谢。

解决方案

首先,你应该考虑不要使用Django模板&所有形式。 (假设你正在开发一些更大的东西)

Django是一个非常酷的框架,但是我发现这两个框架有点有限( https://stackoverflow.com/a/17383408/1432478 )。

您与Yeoman一起使用的JS框架应该负责构建HTML。 / p>

Django-Yeoman集成



开发架构



Yeoman应该提供html(模板替换)和其他静态文件+照顾您选择的JS框架。

如果前端需要从后端获取/提交一些数据,则应发出请求正在提供静态内容的服务器非常相同(Yeoon正在使用Node.js作为此服务器)。



但是等待Node.js服务器如何知道后端逻辑?

哟可以使用 grunt-connect-proxy 将任何请求转发到另一个服务器 - 在这种情况下,Django。

我发现这个 grunt-connect-代理设置指南特别有帮助。



通过向相同的套接字地址( IP:port )发出后端请求,您不必执行CORS或疯狂的东西,如解析整个您的前端代码,以构建您的应用程序的生产就绪版本,以替换开发中使用的套接字地址与适合生产的。



Production&部署



当您运行 grunt 时,将在<$ c中打包您的前端静态文件的生产就绪版本$ c> dist 子目录。

在将django应用程序提交给包之前,您基本上将html和其他静态内容文件复制到 static / your_app

我决定将Angular的html作为静态内容,使Django的模板会引起太大的冲击(冲突的标记,静态装载器等等)。
当部署一些django项目(包含你的django应用程序)时,节点提供的dev设置中的静态文件将由django提供。

换句话说:in生产你只需要一个服务器 - django使用的服务器。



只有在开发过程中,你需要从yeoman提供的模糊的东西中受益,比如:




  • LiveReload

  • 清理代码


  • ...



注意 Yeoman团队正在计划提供一个更好的方式将yeoman与Web框架结合在一起。不确定他们想做什么,也许是类似的发电机解决方案(每个框架单独的发电机)。



样本



您可以查看我目前正在开发的django应用程序: https:// github。 com / vucalur / django-wibses /

它使用Angular JS和以上我刚刚描述了它的架构:)

注意:这是未完成的:)但是截至今天的开发设置已经完成,除了CSRF保护。



Tastypie



我们正在考虑在上述应用程序中使用Tastypie,但只有手动编写RESTful API将变得艰巨。所以我想是否使用Tastypie是否取决于你。



进一步阅读



我基于我的设置在这个



但是,如果您了解Java Web应用程序,您应该看看这些(我希望我的Django-Yeoman集成工作类似于Java(Maven)-Yeoman集成工作):




I'm using Django as backend and Yeoman as frontend. I'm new to both. My frontend local server is running on localhost:9000, and my backend server is running on localhost:8000.

I just built an email sender application following the Django tutorial. It is working perfectly and consists of:

A form:

class ContactForm(forms.Form):
    name = forms.CharField()
    email = forms.EmailField()
    telephoneNr = forms.IntegerField()
    message = forms.CharField(widget=forms.Textarea)

A view:

from django.core.mail import send_mail
from django.shortcuts import render, render_to_response
from django.http import HttpResponseRedirect, HttpResponse
from mailsender.forms import ContactForm

def contact(request):
    if request.method == 'POST': 
        form = ContactForm(request.POST)
        if form.is_valid():
            success = True
            name = form.cleaned_data['name']
            email = form.cleaned_data['email']
            telephoneNr = form.cleaned_data['tlf']
            message= form.cleaned_data['melding']
            receiverEmail = ['somewhere@example.com']
            text = message +'\n'+name +'\n'+str(telephoneNr)

            send_mail('Contact form', beskjed, email, receiverEmail)
            return render(request,"result.html")

    else:
         form = ContactForm(
    return render(request, 'contactform.html', {'form':form})

And my HTML:

<h1>Contact us</h1>

{% if form.errors %}
  <p style="color: red;">
    Please correct the error{{ form.errors|pluralize }} below.
  </p>
{% endif %}

<form action="" method="post">
  <table>
    {{ form.as_p }}
  </table>
  {% csrf_token %}
  <input type="submit" value="Submit">
</form>

If I visit localhost:8000/contactform, the contact form is displayed just as I want, and it does send emails.

I need help figuring out how to hook up this view to the Yeoman frontend - as searching the Internetz (and SO) leads me to the path of confusion. Should I use Tastypie? I really want to keep this logic in the backend. Any help pointing me in the right direction is greatly appreciated.

解决方案

First off, you should consider not using Django templates & forms at all. (Supposing you're working on some bigger stuff)
Django is a really cool framework, but I found those 2 building blocks of it somewhat limited (https://stackoverflow.com/a/17383408/1432478).
The JS framework, that you use with Yeoman should take care of building HTML.

Django-Yeoman integration

Development architecture

Yeoman should serve html (templates replacement) and other static files + take care of JS framework of your choice.
If frontend needs to obtain/submit some data from/to the backend, it should issue a request to the very same server that is serving static content (Under the bonnet Yeoman is using Node.js as this server).

But wait ... how Node.js server is supposed to know about backend logic?
Yo can use grunt-connect-proxy to forward any request to another server - in this case Django.
I found this grunt-connect-proxy setup guide particularly helpful.

By issuing backend requests to the very same socket address (IP:port), you don't have to perform CORS or crazy stuff like parsing whole of your frontend code when building the production-ready version of your app in order to replace the socket address used in development with the one suitable for production.

Production & Deployment

When you run grunt it'll package production-ready version of your frontend static files in dist subdirectory.
Before submitting your django application as a package you basically copy htmls and the rest of the static content files to static/your_app.
I decided to serve Angular's htmls as static content - making them Django's templates would cause too much fuss (conflicting markup, static loaders, and so on ...). When some django project (containing your django app) is deployed, the static files, that were in dev setup served by node, are going to be served by django.
In other words: in production you need only one server - the one used by django.

It's only during development, that you need to benefit from fuzzy-buzzy stuff offered by yeoman, such as:

  • LiveReload
  • linting your code
  • generators
  • ...

Note: I've read that Yeoman team is planning on providing a better way of integrating yeoman with web frameworks. Not sure how they want to do it, perhaps a similar solution to generators (separate generator per framework).

Sample

Yo can check out a django app I'm currently working on: https://github.com/vucalur/django-wibses/
It uses Angular JS and above I just described the architecture of it :)
Note: It is so not-finished-yet :) But as of today development setup is done, except for CSRF protection.

Tastypie

We are considering using Tastypie in the mentioned app, but only when writing RESTful API manually will become arduous. So I think whether to use Tastypie or not is up to you.

Further reading

I based my setup on this.

But if you know something about Java web apps, you should take a look at those (I wanted my Django-Yeoman integration work similarly to how Java(Maven)-Yeoman integration works):

这篇关于Django send_mail应用程序 - 将其与Yeoman前端挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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