简体   繁体   English

Django send_mail应用程序-与Yeoman前端挂钩

[英]Django send_mail application - hook it up with Yeoman frontend

I'm using Django as backend and Yeoman as frontend. 我使用Django作为后端,使用Yeoman作为前端。 I'm new to both. 我都是新来的。 My frontend local server is running on localhost:9000 , and my backend server is running on localhost:8000 . 我的前端本地服务器运行在localhost:9000 ,我的后端服务器运行在localhost:8000

I just built an email sender application following the Django tutorial. 我刚刚按照Django教程构建了一个电子邮件发件人应用程序。 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: 而我的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. 如果我访问localhost:8000/contactformlocalhost:8000/contactform我的意愿显示联系表单,它确实发送电子邮件。

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. 我需要帮助弄清楚如何将这种观点与Yeoman前端挂钩-因为搜索Internetz(和SO)会使我走上混乱的道路。 Should I use Tastypie ? 我应该使用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. 首先,您应该考虑完全不使用Django模板和表单。 (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 ). Django是一个非常酷的框架,但是我发现它的这2个构建块有所限制( https://stackoverflow.com/a/17383408/1432478 )。
The JS framework, that you use with Yeoman should take care of building HTML. 与Yeoman一起使用的JS框架应负责构建HTML。

Django-Yeoman integration Django-Yeoman集成

Development architecture 开发架构

Yeoman should serve html (templates replacement) and other static files + take care of JS framework of your choice. Yeoman应该提供html(模板替换)和其他静态文件+照顾您选择的JS框架。
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). 如果前端需要从后端获取/向后端提交一些数据,它应该向提供静态内容的同一台服务器发出请求(在引擎盖下,Yeoman使用Node.js作为该服务器)。

But wait ... how Node.js server is supposed to know about backend logic? 但是等等... Node.js服务器应该如何了解后端逻辑?
Yo can use grunt-connect-proxy to forward any request to another server - in this case Django. 您可以使用grunt-connect-proxy将任何请求转发到另一台服务器-在这种情况下为Django。
I found this grunt-connect-proxy setup guide particularly helpful. 我发现 grunt-connect-proxy设置指南特别有用。

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. 通过向相同的套接字地址( IP:port )发出后端请求,您无需执行CORS或疯狂的工作,例如在构建应用程序的生产就绪版本以替换套接字时解析整个前端代码。开发中使用的地址与一种适合生产的地址。

Production & Deployment 生产与部署

When you run grunt it'll package production-ready version of your frontend static files in dist subdirectory. 运行grunt它将把生产静态版本的前端静态文件打包在dist子目录中。
Before submitting your django application as a package you basically copy htmls and the rest of the static content files to static/your_app . 在将django应用程序作为软件包提交之前,您基本上将htmls和其余的静态内容文件复制到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 ...). 我决定将Angular的htmls作为静态内容提供-使它们成为Django的模板会引起过多麻烦(标记冲突,静态加载器等)。 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. 当部署了一些django项目(包含django应用程序)时,由node服务的dev设置中的静态文件将由django服务。
In other words: in production you need only one server - the one used by django. 换句话说:在生产中,您只需要一台服务器-django使用的一台服务器。

It's only during development, that you need to benefit from fuzzy-buzzy stuff offered by yeoman, such as: 只有在开发期间,您才需要从yeoman提供的模糊嗡嗡声中受益,例如:

  • LiveReload 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. 注意:我已经读过Yeoman团队正在计划提供一种更好的将yeoman与Web框架集成的方法。 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/ 您可以查看我目前正在使用的Django应用程序: https : //github.com/vucalur/django-wibses/
It uses Angular JS and above I just described the architecture of it :) 它使用了Angular JS,上面我已经描述了它的体系结构:)
Note: It is so not-finished-yet :) But as of today development setup is done, except for CSRF protection. 注意:尚未完成:)但是,到目前为止,除CSRF保护外,开发设置已经完成。

Tastypie Tastypie

We are considering using Tastypie in the mentioned app, but only when writing RESTful API manually will become arduous. 我们正在考虑在提及的应用程序中使用Tastypie,但只有在手动编写RESTful API时才会变得很艰巨。 So I think whether to use Tastypie or not is up to you. 因此,我认为是否使用Tastypie由您决定。

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): 但是,如果您对Java Web应用程序有所了解,就应该看看它们(我希望Django-Yeoman集成的工作方式类似于Java(Maven)-Yeoman集成的工作方式):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM