what's the alternative solution of tornado's "write" in django? - python

I am using django 1.4. I am moving codes from tornado to django. There is self.write() at the end of a class. What 's the alternative solution to self.write()? HttpResponse? There is only one template page
so Do I need to response to other page? can I just call response? or render_to_response to the template page again to wrape data to the page? Is write() same with HttpResponse()?
Hey, guys. there are many "?" above, But I only have one problem. An alternative solution of tornado's "write()" in django.
thx for ur time.
The code in tornado looks like:
class DataHandler(tornado.web.RequestHandler):
...
val = ...
self.write(val)
Maybe in django?
def DataHandler(request):
...
val = ...
return HttpResponse(val)
Is that clear about my question?

HttpResponse is typically used if you wish to return non-template responses.
To render templates, use render from django.shortcuts, for example:
from django.shortcuts import render
def some_handler(request):
context_for_template = {}
return render(request, 'template_name.html', context_for_template)
From Tornado's documentation, write seems to be able to automatically convert a dictionary to JSON. HttpResponse does not do that by default, you should look at Creating a JSON response using Django and Python if it is part of your use case.

the HttpResponse module
from django.http import HttpResponse
def current_datetime(request):
now = datetime.datetime.now()
html = "<html><body>It is now %s.</body></html>" % now
return HttpResponse(html)

Related

Django view that redirects to another domain

How to redirect to another domain with python django and pass additional informations?
For example i want to redirect to https://ssl.dotpay.pl/test_payment/ and give additional informations so url will look like this
https://ssl.dotpay.pl/test_payment/?id=123456&amount=123.00&description=Test
but I don't want to generate url in my view, just pass the data in json or something like that.
What is the way to do this?
this is generated url by what i meant 'ssl.dotpay.pl/test_payment/?id=123456&amount={}&description={}'.format(123.00, 'Test')
Be sure to put a protocol before your url (or at least two slashes).
This way, Django will see it as an absolute path.
From your comment it seems you redirect to ssl.dotpay.pl what will be seen as a local path rather than another domain.
This is what I came across. (See question I put on stackoverflow and answer)
So in your case, you can use the following:
class MyView(View):
def get(self, request, *args, **kwargs):
url = 'https://ssl.dotpay.pl/test_payment/'
'?id=123456&amount={}&description={}'.format(123.00, 'Test')
return HttpResponseRedirect(url)
You could also use redirect from django.shortcuts instead of HttpResponseRedirect
Assuming you are using a functional view, you could probably do something like this in your view:
from django.http import HttpResponseRedirect
def theview(request):
# your logic here
return HttpResponseRedirect(<theURL>)

template.render() python subfunction in Django 1.11 not available

So, I have PyCharm 2017.2.3, and Python 3.6, and Django 1.11. while practicing on the test project, I tried to render to my index.html in view.py under my app. Below is the piece of code that I am talking about:
def index(request):
db_Conn = Album.objects.all()
template = loader.get_template('/music/index.html')
context = {
'db_Conn': db_Conn,
}
return HttpResponse(template.re)
Inside return HttpResponse, I can get until template, but when I use a period after template to use the render() sub-function, I do not get any suggestion from PyCharm for render() instead, I could see two other functions which not relevant to my post.
Can sombody help me on this. My learning is halted due to this.
Try this. I assume music is app name, so your template file should be in, music/templates/index.html
from django.shortcuts import render
from django.utils import timezone
def index(request):
return render(request, 'index.html', {'start_date':timezone.now()})
If not understood refer to this repo

Django: dynamic template rendering

I'm working on a site that has some mostly static content that I still want to use Django template tags in. I don't want to have to write a view and add a urlconf entry for each URL, I just want to add templates to a specific folder and have them be rendered and accessible on the web. Is there already a project out there that does this?
Write a catch all view and resolve template dynamically:
from django.shortcuts import render_to_response
import django.template
import django.http
def view_template(request, template_name):
try:
return render_to_response(template_name)
except django.template.TemplateDoesNotExist:
raise django.http.Http404('No such file: %s' % template_name)
And in your url-conf add, towards the end:
url(r'^/(?P<template_name>[\w-]+\.html)$', view_template),

Django: Getting render_to_response to accept bytes rather than html on disk

I have an html file which is generated on-the-fly, and that I don't want to save to disk. Normally in Django, to get the html to the user, you use:
return render_to_response('foo.html', context_instance=RequestContext(request))
However, I have no foo.html on disk, I have a bytes string. How do I get Django to render a string rather than a file? (Note: The generated file has {% block content %} and so on that Django needs to interpret.
Using python3.4 and Django 1.8.
At cyphase's suggestion: I did the following:
my_template = Template(byte_string)
return HttpResponse(my_template.render(RequestContext(request)))
Try this:
from django.http import HttpResponse
from django.template import Template
def your_view(...):
# Do stuff
yourtemplate = Template(your_template_string)
content = yourtemplate.render(context_instance=RequestContext(request))
# content_type and status are the same as would be passed
# to render() or render_to_response().
return HttpResponse(content, content_type=None, status=None)
Ideally someone would implement a render_to_response() that takes a string or file-like object.

How to redirect with post data (Django)

When processing a POST request in the Django views.py file, I sometimes need to redirect it to another url. This url I'm redirecting to is handled by another function in the same Django views.py file. Is there a way of doing this and maintaining the original POST data?
UPDATE: More explanation of why I want to do this.
I have two web apps (let's call them AppA and AppB) which accept data entered into a text field by the user. When the the user clicks submit, the data is processed and detailed results are displayed. AppA and AppB expect different types of data. Sometimes a user mistakenly posts AppB type data to AppA. When this happens I want to redirect them to AppB and show the AppB results or at least have it populated with the data they entered into AppA.
Also:
The client wants two separate apps rather than combining them into just one.
I can't show the code as it belongs to a client.
UPDATE 2:
I've decided that KISS is the best principle here. I have combined the two apps into one which makes things simpler and more robust; I should be able to convince the client it's the best way to go too. Thanks for all the great feedback. If I was going to maintain two apps as described then I think sessions would be the way to do this - thanks to Matthew J Morrison for suggesting that. Thanks to Dzida as his comments got me thinking about the design and simplification.
If you faced such problem there's a slight chance that you might need to revise your designs.
This is a restriction of HTTP that POST data cannot go with redirects.
Can you describe what are you trying to accomplish and maybe then we can think about some neat solution.
If you do not want use sessions as Matthew suggested you can pass POST params in GET to the new page (consider some limitations such as security and max length of GET params in query string).
UPDATE to your update:)
It sounds strange to me that you have 2 web apps and those apps use one views.py (am I right?). Anyway consider passing your data from POST in GET to the proper view (in case data is not sensitive of course).
I think how I would probably handle this situation would be to save the post data in session, then remove it when I no longer need it. That way I can access the original post data after a redirect even though that post is gone.
Will that work for what you're trying to do?
Here is a code sample of what I'm suggesting: (keep in mind this is untested code)
def some_view(request):
#do some stuff
request.session['_old_post'] = request.POST
return HttpResponseRedirect('next_view')
def next_view(request):
old_post = request.session.get('_old_post')
#do some stuff using old_post
One other thing to keep in mind... if you're doing this and also uploading files, i would not do it this way.
You need to use a HTTP 1.1 Temporary Redirect (307).
Unfortunately, Django redirect() and HTTPResponseRedirect
(permanent) return only a 301 or 302. You have to implement it yourself:
from django.http import HttpResponse, iri_to_uri
class HttpResponseTemporaryRedirect(HttpResponse):
status_code = 307
def __init__(self, redirect_to):
HttpResponse.__init__(self)
self['Location'] = iri_to_uri(redirect_to)
See also the django.http module.
Edit:
on recent Django versions, change iri_to_uri import to:
from django.utils.encoding import iri_to_uri
use requests package.Its very easy to implement
pip install requests
then you can call any urls with any method and transfer data
in your views import requests
import requests
to post data, follow the format
r = requests.post('http://yourdomain/path/', data = {'key':'value'})
to get the absolute url in django view, use
request.build_absolute_uri(reverse('view_name'))
Thus the django view code looks like
r = requests.post(
request.build_absolute_uri(reverse('view_name')),
data = {'key':'value'}
)
where r is the response object with status_code and content attribute.
r.status_code gives the status code(on success it will be 200) and r.content gives the body of response. There is a json method(r.json()) that will convert response to json format
requests
requests.post
Just call your new view from your old view using the same request object.
Of course it won't result in a redirect as such, but if all you care about is 'transferring' data from one view to the other, then it should work.
I tested the following snippet and it works.
from django.views.generic import View
class MyOldView(View):
def post(self, request):
return MyNewView().post(request)
class MyNewView(View):
def post(self, request):
my_data = request.body
print "look Ma; my data made it over here:", my_data
You can use render and context with with it:
Render(request,"your template path", {'vad name' : var value}
You can recive vars in template :
{% If var name %}
{{ var name }}
{% endif %}
I faced a similar issue recently.
Basically I had a form A, upon submitting it another form B would show up, which contains some results + a form. Upon submitting B, i wanted to display some alert to user and keep user on B only.
The way I solved this, is by displaying the results in a <output> field, in B.
<output name="xyz" value="xyz">{{xyz}}</output>
And I used the same view for A->B and B->B. Now I just had to distinguish if the request is coming from A or B and render accordingly.
def view1(request):
if "xyz" in request.POST:
# request from B
# do some processing
return render(request, 'page.html', {"xyz":request.POST["xyz"]})
else:
# request from A
res = foo() # some random function
return render(request, 'page.html', {"xyz":res})
But this only works if form B is small and not that dynamic.
If you are using a redirect after processing the POST to AppB, you can actually get away with calling the AppB method from the AppA method.
An Example:
def is_appa_request(request):
## do some magic.
return False or True
is_appb_request = is_appa_request
def AppA(request):
if is_appb_request(request):
return AppB(request)
## Process AppA.
return HttpResponseRedirect('/appa/thank_you/')
def AppB(request):
if is_appa_request(request):
return AppA(request)
## Process AppB.
return HttpResponseRedirect('/appb/thank_you/')
This should yield a transparent experience for the end-user, and the client who hired you will likely never know the difference.
If you're not redirecting after the POST, aren't you worried about duplicate data due to the user refreshing the page?
You can redirect with session using request.session["key"] as shown below:
# "views.py"
from django.shortcuts import redirect
def my_view(request):
# Here
request.session["message"] = "success"
return redirect("https://example.com")
# "index.html"
{{ request.session.message }} {# success #}

Categories

Resources