django-views

Determine the requested content type?

空扰寡人 提交于 2019-11-29 09:11:06
I'd like to write a Django view which serves out variant content based on what's requested. For example, for "text/xml", serve XML, for "text/json", serve JSON, etc. Is there a way to determine this from a request object? Something like this would be awesome: def process(request): if request.type == "text/xml": pass elif request.type == "text/json": pass else: pass Is there a property on HttpRequest for this? HttpRequest.META , more specifically HttpRequest.META.get('HTTP_ACCEPT') — and not HttpRequest.META.get('CONTENT_TYPE') as mentioned earlier 'Content-Type' header indicates media type

Nested loop in Django template

北城余情 提交于 2019-11-29 07:59:10
I can't get my head around this. I need to somehow access the object in the parent loop but I'm not sure how. Here is what I've come up with so far. I marked the problematic area in the code with XXX : Template: {% for item in ingrcat %} <h2>{{ item.name }}</h2> <ul> {% for ingr in XXX %} <li><a href="#" id="i{{ ingr.id }}">{{ ingr.name }}</a></li> {% endfor %} </ul> {% endfor %} XXX - should be a list of ingredients belonging to the ingredience category which is currently being looped through in the parent loop. View: def home(request): if request.user.is_authenticated(): username = request

How to make a Django Model form Readonly?

[亡魂溺海] 提交于 2019-11-29 07:54:31
I have a django form name "SampleForm". Which i use to take input from user. Now i want to use same form to show this information to user on a different page. But form is editable I want to make the form read only. Is there any way to make whole form Readonly ? François Constant pseudo-code (not tested): class ReadOnlyFormMixin(ModelForm): def __init__(self, *args, **kwargs): super(ReadOnlyFormMixin, self).__init__(*args, **kwargs) for key in self.fields.keys(): self.fields[key].widget.attrs['readonly'] = True def save(self, *args, **kwargs): # do not do anything pass class SampleReadOnlyForm

How do I use UpdateView?

断了今生、忘了曾经 提交于 2019-11-29 07:53:40
I have two, presumably related, problems with UpdateView. First, it is not updating the user but creating a new user object. Second, I cannot restrict the fields displayed in the form. Here is my views.py: class RegistrationView(FormView): form_class = RegistrationForm template_name = "register.html" success_url = "/accounts/profile/" def form_valid(self, form): if form.is_valid: user = form.save() user = authenticate(username=user.username, password=form.cleaned_data['password1']) login(self.request, user) return super(RegistrationView, self).form_valid(form) #I still have no idea what this

How to set cookie in Django and then render template?

╄→尐↘猪︶ㄣ 提交于 2019-11-29 06:17:37
问题 I want to set a cookie inside a view and then have that view render a template. As I understand it, this is the way to set a cookie: def index(request): response = HttpResponse('blah') response.set_cookie('id', 1) return response However, I want to set a cookie and then render a template, something like this: def index(request, template): response_obj = HttpResponse('blah') response_obj.set_cookie('id', 1) return render_to_response(template, response_obj) # <= Doesn't work The template will

Django passing variables to templates from class based views

我怕爱的太早我们不能终老 提交于 2019-11-29 06:07:22
问题 If I have a class based view, like this, class SomeView (View): response_template='some_template.html' var1 = 0 var2 = 1 def get(self, request, *args, **kwargs): return render_to_response(self.response_template, locals(), context_instance=RequestContext(request)) My question is, inside the template some_template.html , how do I access var1 and var2 ? As far as I understood this, the locals() sort of just dumps all the local variables into the template, which has worked very well so far. But

__init__() got an unexpected keyword argument 'user'

限于喜欢 提交于 2019-11-29 05:27:13
问题 i am using Django to create a user and an object when the user is created. But there is an error __init__() got an unexpected keyword argument 'user' when calling the register() function in view.py. The function is: def register(request): '''signup view''' if request.method=="POST": form=RegisterForm(request.POST) if form.is_valid(): username=form.cleaned_data["username"] email=form.cleaned_data["email"] password=form.cleaned_data["password"] user=User.objects.create_user(username, email,

'RelatedManager' object is not iterable Django

谁都会走 提交于 2019-11-29 05:23:55
Hey i have looked around through some simliar posts here on SO but havent found anything that has solved my problem. I have the following models, from django.db import models class Areas(models.Model): name = models.CharField(max_length = 120) order_in_sidebar_network = models.IntegerField(blank=True, null=True) order_in_section_network = models.IntegerField(blank=True, null=True) def __unicode__ (self): return self.area_name class Meta: verbose_name_plural = "Areas" verbose_name = "Area" class Countries(models.Model): name = models.CharField(max_length = 120, help_text = "The name of the

Django check if object in ManyToMany field

人盡茶涼 提交于 2019-11-29 04:24:22
问题 I have quite a simple problem to solve. I have Partner model which has >= 0 Users associated with it: class Partner(models.Model): name = models.CharField(db_index=True, max_length=255) slug = models.SlugField(db_index=True) user = models.ManyToManyField(User) Now, if I have a User object and I have a Partner object, what is the most Pythonic way of checking if the User is associated with a Partner? I basically want a statement which returns True if the User is associated to the Partner . I

Django FileField upload is not working for me

房东的猫 提交于 2019-11-29 03:52:37
I have been scratching my head FileField. Does the FileField require a seperate process? Although my url gets saved .. but my file doesn't get uploaded ... what am i doing wrong? This is my models.py ... class OpLink(models.Model): user = models.ForeignKey(User) file = models.FileField(blank=True, null=True, upload_to="uploads") url = models.URLField(blank=True, null=True) my forms.py class OpLinkForm(ModelForm): class Meta: model = OpLink exclude = ('user') my views.py oplinkform = oplinkform(request.POST) oplink = oplinkform.save(commit=False) oplink.user = user oplink.save() and my html to