django-views

Django pass object from view to next for processing

点点圈 提交于 2019-12-03 13:24:24
If you have 2 views, the first uses modelform that takes inputted information from the user (date of birth, name, phonenumber, etc), and the second uses this information to create a table. How would you pass the created object in the first view to the next view so you can use it in the second view's template I'd appreciate any help you can share One approach is to put the object into a session in your first view, which you could then retrieve from the request.session in the second view. def first_view(request): my_thing = {'foo' : 'bar'} request.session['my_thing'] = my_thing return render

How to stream opencv frame with django frame in realtime?

浪尽此生 提交于 2019-12-03 13:00:33
I'm trying to use raspberry pi capture the image from USB camera and stream it with Django framework I have tried to use StreamingHttpResponse to stream the frame from Opencv2. However, it just shows 1 frame and not replacing the image. How can I replace the image in real time? Here is my code. from django.shortcuts import render from django.http import HttpResponse,StreamingHttpResponse import cv2 import time class VideoCamera(object): def __init__(self): self.video = cv2.VideoCapture(0) def __del__(self): self.video.release() def get_frame(self): ret,image = self.video.read() ret,jpeg = cv2

New chat message notification Django Channels

对着背影说爱祢 提交于 2019-12-03 12:22:56
I've got Django Channels 2.1.2 set up in my Django app by following a tutorial and now need to set up a notification system for new messages. I want to do this in the simplest way possible. I can do it via browser push notifications, but I don't want to do it like that. I want it to be like Stack Overflow, where there is a red number representing the instance of a new message. One answer on here said For notifications you only need two models: User and Notification . On connect set the scope to the currently authenticated user. Set up a post_save signal on your Notification model to trigger a

What's the difference between returning a `HttpResponseNotFound` and raising a `Http404` in Django?

こ雲淡風輕ζ 提交于 2019-12-03 09:43:43
There are apparently two different ways to return a 404 error in Django: by returning a HttpResponseNotFound object or by raising an Http404 exception. While I'm using the former in my project, it seems that Django's internal views are mostly using the latter. Apart from the "Exception is exceptional" mantra, what's the difference between both ways and which should I be using? An HttpResponseNotFound is just like a normal HttpResponse except it sends error code 404. So it's up to you to render an appropriate 404 page in that view, otherwise the browser will display its own default. Raising an

How to cache a paginated Django queryset

半城伤御伤魂 提交于 2019-12-03 09:13:43
How do you cache a paginated Django queryset, specifically in a ListView? I noticed one query was taking a long time to run, so I'm attempting to cache it. The queryset is huge (over 100k records), so I'm attempting to only cache paginated subsections of it. I can't cache the entire view or template because there are sections that are user/session specific and need to change constantly. ListView has a couple standard methods for retrieving the queryset, get_queryset() , which returns the non-paginated data, and paginate_queryset() , which filters it by the current page. I first tried caching

Django: What's the use of the context_instance parameter in the render shortcut function?

孤者浪人 提交于 2019-12-03 09:03:11
问题 Documentation on 'Render' shortcut According to the link above, the context_instance parameter is defined as The context instance to render the template with. By default, the template will be rendered with a RequestContext instance (filled with values from request and dictionary). With this definition in mind, I don't see any scenarios that would benefit from supplying the context_instance argument. I mean if I need to provide additional context values I would just add them to the dictionary

How to display custom 404.html page in Django

ぃ、小莉子 提交于 2019-12-03 09:01:50
I want to display custom 404 error page when end user enters wrong url,I have tried but i am getting only Django default 404 page.I am using Python(2.7.5),Django(1.5.4) My Code urls.py from django.conf.urls import patterns, include, url from mysite import views handler404 = views.error404 urlpatterns = patterns('', url(r'^$', 'mysite.views.home', name='home'), ) views.py from django.http import HttpResponse from django.shortcuts import render from django.template import Context, loader def home(request): return HttpResponse("welcome to my world") def error404(request): template = loader.get

Django - Ajax registration

南笙酒味 提交于 2019-12-03 08:48:27
I am trying to allow registration (using this django-registration register view) to one of my applications from a modal dialog. Since this form is in a modal box, I'd like to get an json reponse on success (instead of the default redirection) How can I use this view ( django-registration register ) to manage the registration and send back a json response on success ? I know how to make ajax/json responses, the question is how to use the django-registration view without the redirection behavior or wrap it into an other view to manage the response. First you need to change the urls.py to wrap

Can I redirect to another url in a django TemplateView?

烈酒焚心 提交于 2019-12-03 08:37:29
问题 I have a url mapping that looks like this: url(r'^(?P<lang>[a-z][a-z])/$', MyTemplateView.as_view()), There are only a few values that I accept for the lang capture group, that is: (1) ro and (2) en . If the user types http://server/app/fr/ , I want to redirect it to the default http://server/app/en/ . How can I do this since MyTemplateView only has a method that is expected to return a dictionary? def get_context_data(self, **kwargs): return { 'foo': 'blah' } 回答1: I know this question is old

Django: Breaking up views

北战南征 提交于 2019-12-03 08:33:49
问题 This is really just a "best practices" question... I find that When developing an app, I often end up with a lot of views. Is it common practice to break these views up into several view files? In other words... instead of just having views.py, is it common to have views_1.py, views_2.py, views_3.py (but named more appropriately, perhaps by category)? 回答1: Splitting views.py Most of your code probably expects your views to be accessible as myapp.views.viewname . One way I've seen people break