django-views

How do I change a Django Template based on the User's Group?

我与影子孤独终老i 提交于 2019-11-29 03:41:34
问题 Right now I have two different groups of users on my site: customers and businesses. Right now I am only using one login that allows both user groups to see their profile page. However, there are portions of the profile page I only want the Customer to see and portions I only want the business to see. How can I go about limiting what each group sees on this page? Should I do it in the template with some sort of if statement? or is there some other solution anyone can let me know about? 回答1:

django - django-taggit form

我的未来我决定 提交于 2019-11-29 03:19:40
问题 I would like to use django-taggit (click here ). The documentation ( click here) talks about using ModelForm to generate the form but I have already my form that I would like to use. Let's say if I have something like this: forms.py class MyForm(forms.Form): ...... tags = forms.CharField(max_length=200, widget=forms.Textarea) how do I save the the tags coming from the tags field? What goes in my views.py ? A real example would be truly appreciated. 回答1: I'm not too familiar with the django

Forbidden (403) CSRF verification failed. Request aborted

本小妞迷上赌 提交于 2019-11-29 02:09:01
I am making an app of login form but when I am running my app and click on login button the following error will occur Forbidden (403) CSRF verification failed. Request aborted. the code of view.py is as: from django.template import loader from django.shortcuts import render_to_response from registration.models import Registration from django.http import HttpResponse from django.template import RequestContext from django.shortcuts import redirect def view_login(request,registration_id): t = loader.get_template('registration/login.html') try: registration=Registration.objects.get(pk

Django UpdateView without pk in url

南笙酒味 提交于 2019-11-29 01:56:35
Is it possible eliminate pk from url related to UpdateView ? For example, if I have url(r'^myobj/update/(?P<pk>\d+)/$', views.UpdateMyObj.as_view(), name="update") is there any way to write it like url(r'^myobj/update/$', views.UpdateMyObj.as_view(), name="update") and then send pk as a parameter in POST or GET request? Yes it is possible you just need to override the get_object method: from django.views.generic.edit import UpdateView class UpdateMyObj(UpdateView): # ..... def get_object(self): return MyModel.objects.get(pk=self.request.GET.get('pk')) # or request.POST 来源: https:/

Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found

江枫思渺然 提交于 2019-11-29 01:30:44
I'm following the official tutorial to learn Django and using 1.5. I had this link as part of my index template, which was working fine: <li><a href="/polls/{{ poll.id }}/">{{ poll.question }}</a></li> however, this is hardcoded and the tutorial suggested a better way was to use: <li><a href="{% url 'detail' poll.id %}">{{ poll.question }}</a></li> so that you'll be better of when dealing with huge number of templates and u have to make changes to the url. Since I made the above change I get the following errors when I run the app: Exception Type: NoReverseMatch Exception Value: Reverse for

Django serve static index.html with view at '/' url

雨燕双飞 提交于 2019-11-29 01:27:53
问题 I have my index.html in /static/ folder. My django app is running ok when i try: http://127.0.0.1:8000/index.html But i want to acces index.html by url: http://127.0.0.1:8000/ I wrote a view and it works: class IndexView(TemplateView): template_name = 'index.html' I also added to urls.py(this lets me serve static like http://127.0.0.1:8000/css/style.css ): url(r'^(?P<path>.*)$', 'django.contrib.staticfiles.views.serve', { 'document_root': settings.STATIC_ROOT, 'show_indexes':True }), But i

How can i pass data to django layouts (like 'base.html') without having to provide it through every view?

妖精的绣舞 提交于 2019-11-29 01:19:30
问题 I am trying to pass the data to layout 'base.html' . I am currently doing it by storing the data in request.session and accessing it in 'base.html' through request object. Is there any way to pass the data to 'base.html' without having to pass the data from every views? 回答1: Use a context processor, which is made exactly for that purpose. Create a file context_processors.py in one of your app directories, then in the file define a function that return a dictionary of variables to insert in

Why on earth do I have to pass RequestContext in all of my responses?

拥有回忆 提交于 2019-11-29 01:13:45
问题 I want to highlight the current page in the navigation menu. Obviously I need to give the menu links a class like 'active' when you are on their page. This is a classic problem and I've seen many solutions proposed. My problem is I hate all of them and consider none of them to be very DRY. For example: @register.simple_tag def active(request, pattern): import re if re.search(pattern, request.path): return 'active' return '' ---- {% load tags %} <div id="navigation"> <a class="{% active

Django per user view caching

穿精又带淫゛_ 提交于 2019-11-29 01:12:35
问题 I need a per user caching. The regular view caching does unfortunately not support user-based caching. I tried the template fragment caching like this: {% load cache %} {% cache 500 "mythingy" request.user %} ... HTML stuff ... {% endcache %} but it's slow as hell. Does anybody know a faster way to achieve what I need? Thanks! 回答1: as of django >=1.7, using the cache_page along with vary_on_cookie decorators on your view should solve this. something like this. from django.views.decorators

Django: Adding inline formset rows without javascript

心已入冬 提交于 2019-11-29 00:38:35
This post relates to this: Add row to inlines dynamically in django admin Is there a way to achive adding inline formsets WITHOUT using javascript? Obviously, there would be a page-refresh involved. So, if the form had a button called 'add'... I figured I could do it like this: if request.method=='POST': if 'add' in request.POST: PrimaryFunctionFormSet = inlineformset_factory(Position,Function,extra=1) prims = PrimaryFunctionFormSet(request.POST) Which I thought would add 1 each time, then populate the form with the post data. However, it seems that the extra=1 does not add 1 to the post data.