I have a django project.
index.html page of it contains a form with submit button. I want to include django-template in this index.html page to send email with data from this form. Is it possible to do without any python-code? Just like:
{% send_mail( {{ request.GET.to }}, {{ request.GET.subject }}, {{ request.GET.message }} %}
P.S. I am new to django.
This wont work. Django template is not designed to call python modules/methods directly.
You can either create a template tag, or on click, trigger a url/view that would send the email.
From the philosophy of django template language
If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.
Documentation on how to create template tags
And here is a nice tutorial talking about the email sending functionality through views.py
No, that's not possible. In order for those values request.GET.to to be populated, you have to click the submit button - which has to go somewhere.
Now, if you really must - you can do this strictly from the browser using javascript and a library like mailgun which works over HTTP.
Simply create a request just like you would any AJAX request, read the contents of the form fields and your email message will be sent - without ever touching your server-side code.
Related
I have this "Log in" page.
I wanted to make it so that, if the user clicks "Log In" and all the input fields aren't filled or error or something that it would change the input border color red and shadow.
I'm using nodejs express ig. and its in a form I'm using method post. I tried getting stuff with "document.." but that doesn't work. any solutions?
JavaScript running in the browser can manipulate the DOM via the API provided through the document object.
On the server there is no document. The browser makes an HTTP request. The server generates an HTTP response. When the response is HTML it is usually generated from either a static file or a template.
If you want to dynamically modify the document, a template is the best way to do this.
You could do something along the lines of:
<input name="something" class="<%= fields_with_errors.includes('something') ? 'errors' : '' %>" value="<%= user_input.something %>">
Where fields_with_errors is an array and user_input is an object holding the values the user entered when they submitted the form.
The specifics depend on the template language you pick.
There are numerous template languages you can use with Node.js. Pick one and integrate it with the code you use to process and response to HTTP requests. Many people use Express to do this. MDN has a tutorial. There are lots of middlewares to add template support to Express and the MDN tutorial covers them.
I have a quick question. I am currently developing a React app with Python on the backend. On the Python side, I parse some html files, and I extract some html tags from them. Then it gets packed into a JSON and sent to the frontend. So React app receives something like this:
{'<b>id</b>': 'Link'}
So those are like raw html tags in form of strings. Now I want to render them. And this happens:
And this is how it looks under dev tools:
So it gets rendered as literal string and what I want is the text on the left side to be bold, and the text on the other side to be an actual hyperlink. Is there a way to do it?
React stops this intentionally. See the docs.
In general, setting HTML from code is risky because it’s easy to inadvertently expose your users to a cross-site scripting (XSS) attack.
You can still accomplish it by using dangerouslySetInnerHTML. Replace the following test div with your variable holding html.
const test = "<div>TEST</div>";
return <div dangerouslySetInnerHTML={{__html: test }} />;
In a Django view, I'm placing an Html form (actual string with some Html, from my DB) into an Html template, with a tag: {{ injected_form|safe }}. This succeeds in showing the form, but I've got an issue POSTing it:
Forbidden (...) CSRF token missing or incorrect.
(Which I know is appropriate Django behaviour, as I have no CSRF token tag/field inside the form itself. Btw the reason for the custom Html form strings in the DB is that they can be produced by the actual user)
A solution I could implement is TheBronx's answer to a question here. This seems to be a case where just doing
<form method="post">
{% csrf_token %}
......
</form>
is not possible! Are there solutions for this issue?
I've figured out how to handle/receive POSTs without a related Django model, but I didn't foresee this CSRF problem submitting them :( Any help would be greatly appreciated
I'm not sure why it isn't possible. Surely the form stored in the database doesn't need to include the <form> tags themselves, so you could easily use those yourself and add the CSRF token. That seems safer anyway, since you should really ensure the destination of the form POST yourself.
But I must say, this whole approach seems wrong. It's never really going to be safe to allow users to add raw HTML to your database and output it directly, unescaped, to the template. And allowing them to specify form fields in HTML seems like a recipe for all sorts of injection attacks.
Instead consider allowing them to choose from a selection of fields, and build up the form yourself from those.
i have this question and i can't find a solution. I'm pretty new in the programming. So..
my HTML file with the form is contact.html (straight html file)
The php code is in separated file. So, everythings work fine, but..
I want to have the return text for the "success" or "fail" in the in my html file (let's say under the submit button). Is it possible if it's not php file with html code, but straight html. And can you tell me what should I do?
I hope you understand me.
Straight HTML can't give different content conditionally.
You could get something that resembles what you describe by placing an iframe after the form and targeting it with the form. As solutions go, that's rather ugly.
You could use JavaScript to send the form data to the PHP file and then modify the DOM of the HTML document based on the response (Ajax), but that would be unnecessarily complex and violate the best practise of unobtrusive JavaScript.
The usual approach to this problem is to have a PHP file being the single entry point for all things relating to the form (it can keep parts of its logic or HTML in other files and include() them as needed) and have it decide, based on what input receives, if it should show:
The plain form
The form with error messages (and pre-populated with the user's incorrect submitted data)
The thank-you
Are there examples of sending simple HTML formatted emails (<h1>, <b> and such) out from Plone?
Plain text is well-covered, but there are no HTML email out examples.
You can adapt any of the many python email module examples. Because HTML email usually means sending multipart/alternative messages, things get a little more complicated.
The examples page of the email package.
Sending HTML email using Python
Sending Multipart html emails which contain embedded images
You basically have to construct an email.Message object and pass that to Mailhost.send.
Depending on your use case, you could also use collective.watcherlist.
This was factored out of Products.Poi, which uses it to allow users to subscribe to updates for an issue. That part may not be interesting for you, but it has code that takes a browserview as basis for sending an email. Hooking a page template up to that browserview is of course simple.
If you cannot use it directly, it may serve as a code example.