django-views

How to do a DetailView in django 1.3?

老子叫甜甜 提交于 2019-12-03 00:40:37
I'm currently learning how to use the class-based views in django 1.3. I'm trying to update an application to use them, but I still don't uderstand very well how they work (and I read the entire class-based views reference like two or three times EVERY day). To the question, I have an space index page that needs some extra context data, the url parameter is a name (no pk, and that can't be changed, it's the expected behaviour) and the users that don't have that space selected in their profiles can't enter it. My function-based code (working fine): def view_space_index(request, space_name):

How to specify the login_required redirect url in django?

这一生的挚爱 提交于 2019-12-02 23:15:17
I have a view function: @login_required def myview(): # do something # respond something pass How can I specify the exact URL for this view function to be redirected? Bob LOGIN_URL in your settings Reference: LOGIN_URL LOGIN_REDIRECT_URL you can do this in your view works fine for me without declaring in settings.py from django.contrib.auth.decorators import login_required @login_required(login_url='/example url you want redirect/') #redirect when user is not logged in def myview(request): do something return something #returns when user is logged in default login url is: '/accounts/login/' if

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

▼魔方 西西 提交于 2019-12-02 23:09:48
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 parameter. I don't know how context_instance can be useful. Please educate me. Thanks. The main

How can i display my data in database and export it to pdf -Django

扶醉桌前 提交于 2019-12-02 22:52:29
问题 my x variable is getting all the data in my database, i guess? someone help me how can i display all data and export it to pdf file. response = HttpResponse(content_type='application/pdf') response['Content-Disposition'] = 'attachment; filename="WishList.pdf"' buffer = BytesIO() # Create the PDF object, using the BytesIO object as its "file." p = canvas.Canvas(buffer) x = Item.objects.all() p.drawString(100, 100,x) p.drawString(200,300,"sad") # Close the PDF object cleanly. p.showPage() p

Can I redirect to another url in a django TemplateView?

馋奶兔 提交于 2019-12-02 22:25:51
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' } I know this question is old, but I've just done this myself. A reason you may think you want to do it in get_context_data is due to

How to add extra data to a Django MPTT model for display in template?

喜夏-厌秋 提交于 2019-12-02 21:57:58
问题 This question is related to this one. My Django Model: from mptt.models import MPTTModel, TreeForeignKey class myModel(MPTTModel): myIntA = models.IntegerField(default=0) myParent = TreeForeignKey('self', null=True, blank=True, related_name='children') My View: myModelList = myModel.objects.all() for i in range(len(myModelList)): myModelList[i].myIntB = i return render( request, 'myApp/myTemplate.html', Context( { "myModels": myModelList, } ) ) Is the above legal? You can see that I added a

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

余生长醉 提交于 2019-12-02 21:57:06
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: <WSGIRequest path:/devices/1/, GET:<QueryDict: {u'pk': [u'1']}>, As far as I can tell, that should be all that

Django test RequestFactory vs Client

半世苍凉 提交于 2019-12-02 21:41:28
I am trying to decide whether I should use Django's Client or RequestFactory to test my views. I am creating my server using DjangoRESTFramework and it's really simple, so far: class SimpleModelList(generics.ListCreateAPIView): """ Retrieve list of all route_areas or create a new one. """ queryset = SimpleModel.objects.all() serializer_class = SimpleModelSerializer filter_backends = (IsOwnerFilterBackend,) def perform_create(self, serializer): serializer.save(owner=self.request.user) What are the differences between testing with Django's Client and RequestFactory and which approach is more

How to use Pusher with Django?

妖精的绣舞 提交于 2019-12-02 21:15:15
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! Pusher allows you to easily implement a publish/subscribe pattern for messaging (also called pub/sub for short). In this pattern, there are a number of channels. Each channel is like a radio station's frequency. A publisher puts messages on a

Django models are not ajax serializable

偶尔善良 提交于 2019-12-02 21:04:45
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"} Why can't simplejson parse my one of the default django models? I get the same behavior with any