django-views

Multiple models generic ListView to template

巧了我就是萌 提交于 2019-12-03 08:30:34
What is the SIMPLEST method for getting 2 models to be listed in a generic IndexView? My two models are CharacterSeries and CharacterUniverse . My views.py from .models import CharacterSeries, CharacterUniverse class IndexView(generic.ListView): template_name = 'character/index.html' context_object_name = 'character_series_list' def get_queryset(self): return CharacterSeries.objects.order_by('name') class IndexView(generic.ListView): template_name = 'character/index.html' context_object_name = 'character_universe_list' def get_queryset(self): return CharacterUniverse.objects.order_by('name') I

How do I pass a PK or slug to a DetailView using RequestFactory in Django?

戏子无情 提交于 2019-12-03 08:16:54
问题 I'm trying to use RequestFactory to test a DetailView with the following test case: def test_device_homepage(self): request = self.factory.get('/devices/1/', {'pk': 1}) response = DeviceView.as_view()(request) self.assertEqual(response.status_code, 404) When I run the above test, however, I get the following error message: AttributeError: Generic detail view DeviceView must be called with either an object pk or a slug. If I print the request after creation, I can see the following:

How do I jQuery ajax live search for the models in Django?

风流意气都作罢 提交于 2019-12-03 08:04:06
I tried live search with jquery and ajax, and even posted a question regarding this here , but there seems to have some serious problem somewhere in my view or in the ajax script I wrote. It searches and loads the content correctly. But if I backspace and there's no value in the search form, it still shows me a list of value that I entered the first time. I think there's really a big problem in my code. models.py: class Status(models.Model): status = models.TextField() image = models.ImageField(upload_to=get_upload_file_name, blank=True) pub_date = models.DateTimeField(default=datetime.now)

How to use two different Django Form at the same template?

雨燕双飞 提交于 2019-12-03 07:59:08
My forms.py: class AlertForm(forms.ModelForm): class Meta: model=Alert fields = ('high','medium', 'user') widgets = { 'user': forms.HiddenInput() } AlertCountFormset = modelformset_factory(Alert, form = AlertForm) Another Django Form class: class NotifierForm(forms.ModelForm): high = forms.ChoiceField(choices=NOTIFIER_TYPE) medium = forms.ChoiceField(choices=NOTIFIER_TYPE) low = forms.ChoiceField(choices=NOTIFIER_TYPE) def save(self, commit=True): alert = super(NotifierForm, self).save(commit=False) alert.high = self.cleaned_data["high"] alert.medium = self.cleaned_data["medium"] alert.low =

How to use Pusher with Django?

。_饼干妹妹 提交于 2019-12-03 07:49:49
问题 I am trying to build an app using pusher and django. I went through few of the links like https://github.com/pusher/django-pusherable, but it lacked an example and thus was difficult to understand! Can anyone please help in here? And also what are channels in here and thus how to create a follow-following system with feeds(activity streams)? Thanks! 回答1: Pusher allows you to easily implement a publish/subscribe pattern for messaging (also called pub/sub for short). In this pattern, there are

Get the click event from a button in a django view

我是研究僧i 提交于 2019-12-03 07:48:39
i think the title is pretty clear. I want to know when the user clicks the button to run a piece of code in a function in my views.py. lets say i have this html: <div> <input type="button" name="_mail" value="Enviar Mail"> </div> and i want to run this code if the user clicks on it: send_templated_mail(template_name='receipt', from_email='robot@server.com', recipient_list=[request.user.email], context=extra_context) that´s all i want to do. EDIT : this is the view that i have: def verFactura(request, id_factura): fact = Factura.objects.get(pk = id_factura) cliente = Cliente.objects.get(factura

Django models are not ajax serializable

丶灬走出姿态 提交于 2019-12-03 07:34:52
问题 I have a simple view that I'm using to experiment with AJAX. def get_shifts_for_day(request,year,month,day): data= dict() data['d'] =year data['e'] = month data['x'] = User.objects.all()[2] return HttpResponse(simplejson.dumps(data), mimetype='application/javascript') This returns the following: TypeError at /sched/shifts/2009/11/9/ <User: someguy> is not JSON serializable If I take out the data['x'] line so that I'm not referencing any models it works and returns this: {"e": "11", "d": "2009

How to redirect users to a specific url after registration in django registration?

Deadly 提交于 2019-12-03 06:54:18
So I am using django-registration app to implement a user registration page for my site. I used Django's backends.simple views which allows the users to immediately login after registration. My question is how do I redirect them to my other app's page located in the same directory as the project. Here is what my main urls.py looks like: from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', url(r'^accounts/', include('registration.backends.simple.urls')), url(r'

Django: return a StreamingHttpResponse on an existing html page

我是研究僧i 提交于 2019-12-03 06:48:28
Since it is better to have a single question for each issue, be patient if is similar to another part of another my question related to the same project. The situation: I have a form on html in which I can set a number and when it is submitted, it is call views.stream_response which pass the value to stream.py and it returns a StreamingHttpResponse and "virtual" blank browser page appears ( /stream_response/ ) in which I can see a progressive number every second up to m : 1 2 3 .. m stream.py import time def streamx(m): lista = [] x=0 while len(lista) < m: x = x + 1 time.sleep(1) lista.append

How do I use Logging in the Django Debug Toolbar?

送分小仙女□ 提交于 2019-12-03 06:30:21
问题 I would like to output debug messages in my django app at different points in a view function. The docs for the django-debug-toolbar say it uses the build in python logging but I can't find any more information then that. I don't really want to log to a file but to the info pane on the toolbar. How does this work? 回答1: You just use the logging module methods and DjDT will intercept and display them in the Logging Panel. import logging logging.debug('Debug Message') if some_error: logging