django-views

Django - short non-linear non-predictable ID in the URL

不羁岁月 提交于 2019-12-05 00:33:11
问题 I know there are similar questions (like this, this, this and this) but I have specific requirements and looking for a less-expensive way to do the following (on Django 1.10.2): Looking to not have sequential/guessable integer ids in the URLs and ideally meet the following requirements: Avoid UUIDs since that makes the URL really long. Avoid a custom primary key. It doesn’t seem to work well if the models have ManyToManyFields. Got affected by at least three bugs while trying that (#25012,

Using fullCalendar in Django

一曲冷凌霜 提交于 2019-12-04 23:21:50
Somewhat of a follow-up to the question posted here ( Django: modifying data with user input through custom template tag? ), but since asking the question I've decided to take a different approach. As you can tell I'm a newb so please go easy. I want a weekly calendar in my Django app that displays shifts in the database. Following is the Shift model. class Shift(models.Model): owner = models.ForeignKey(settings.AUTH_USER_MODEL, related_name='shifts', blank=True, null=True) # A shift may not have an owner. # Choices for fields. Add more choices as necessary. DAYS = ( ('M', 'Monday'), ('TU',

Raise 404 and continue the URL chain

谁说胖子不能爱 提交于 2019-12-04 22:16:24
I've got a URLs pattern like this: urlpatterns = ( url(r'^$', list_titles, name='list'), url(r'^(?P<tag>[a-z\-0-9]+?)/$', list_titles, name='filtered-list'), url(r'^(?P<title>\S+?)/$', show_title, name='title'), ) The filtered-list and title match the same things. If there is an available list of things matching the tag in filtered-list , I want list_titles to fire off. But if there isn't a matching tag , I want to bubble that back to the URL processor so show_title fires off. If there's no matching title, I'll raise a proper 404 there. I know I can do this from inside the view...but it's a

django: return string from view

谁都会走 提交于 2019-12-04 22:12:21
I know this is a simple question, sorry. I just want to return a simple string, no templates. I have my view: def myview(request): return "return this string" I don't remember the command. Thanks According to the documentation : A view function, or view for short, is simply a Python function that takes a Web request and returns a Web response. Each view function is responsible for returning an HttpResponse object. In other words, your view should return a HttpResponse instance: from django.http import HttpResponse def myview(request): return HttpResponse("return this string") ThePhi You can't

'CheckoutView' object has no attribute 'object'

给你一囗甜甜゛ 提交于 2019-12-04 22:08:07
问题 I am getting no attribute 'object' error' here is views.py class CheckoutView(FormMixin , DetailView): model = Cart template_name = "carts/checkout_view.html" form_class = GuestCheckoutForm def get_object(self , *args , **kwargs): if self.request.user.is_authenticated(): try: cart = Cart.objects.get(user__username=self.request.user) except: cart = None if cart == None: HttpResponseRedirect(reverse("cart")) else: cart_id = self.request.session.get("cart_id") if cart_id == None:

Display table of objects django

巧了我就是萌 提交于 2019-12-04 21:45:04
问题 I need to display a table from my database with Django. The obvious way is to manually type in the table headings and loop through query results of model.objects.all() . However, being quite lazy, I want to do this automatically, i.e. load all fields from model through introspection to display as column headings and load all field values to display as rows. This approach can also save me some time later because I don't have to update my template code when my model changes. I got it to work

How to pass parameters to django generic views

做~自己de王妃 提交于 2019-12-04 21:17:18
I would like to pass a number to my generic view (DetailView) to get one object Here is my code Urlpattern (r'^newreportview/(?P<number>\w+)/$', NewReportView.as_view()), View Class class NewReportView(DetailView): template_name = "report/newreportview.html" context_object_name = "newreportview" def get_queryset(self): task= get_object_or_404(MyTask,applicationnnumber=self.args[0]) return task I guess something is wrong in this line name = get_object_or_404(MyTask,applicationnnumber=self.args[0]) error message: Exception Type: IndexError Exception Value: tuple index out of range How should I

How to play a audio file through http response in django(python)

[亡魂溺海] 提交于 2019-12-04 20:55:56
I want to make request to url and django view should read the file and send the http response back to play the same file in browser.I got the following code but it does't play anything please anyone help me.. Right now i hard coded the file name in the code. url: http://localhost/playfile/audiofile_name def playAudioFile(request): try: fname="C:\\test\\audio\\t.mp3" wrapper = FileWrapper(file(fname)) print content_type response = HttpResponse(wrapper, content_type="audio/mpeg") print response response['Content-Length'] =os.path.getsize(fname ) return response except: return HttpResponse()

Django-formwizard and ModelFormSet save

安稳与你 提交于 2019-12-04 20:24:48
I am rewriting a big piece of our application which requires a user to create a Project with Rewards attached to it. The form is broken into different steps, the first two are the normal Project , the next one is the Rewards , and then lastly a simple preview that lets the user flick back and forth to create a perfect Project . my forms.py class BaseRewardFormSet(BaseModelFormSet): def __init__(self, *args, **kwargs): super(BaseRewardFormSet, self).__init__(*args, **kwargs) self.queryset = Reward.objects.none() RewardFormSet1 = modelformset_factory(Reward, extra=2, exclude=('project'), formset

How to stream opencv frame with django frame in realtime?

ぐ巨炮叔叔 提交于 2019-12-04 20:10:17
问题 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