django-views

ValueError: too many values to unpack (expected 2) in Django

别等时光非礼了梦想. 提交于 2019-12-09 16:48:21
问题 I am reorganizing one of my projects to be more re-usable and just generally structured better and am now getting the error below whenever I run makemigrations - I've spent half the day trying to figure this out on my own but have run out of Google results on searches and am in need of some assistance. What I've done was remove a custom user model I had setup so I can use Django's built-in User model and I also namespaced my apps urls. I don't want to include a bunch of code yet that will do

Django - MEDIA_ROOT and MEDIA_URL

China☆狼群 提交于 2019-12-09 15:42:08
问题 Could you please suggest me the good documentation where I can understand the MEDIA_URL and MEDIA_ROOT? It's really confusing me. 回答1: The MEDIA_ROOT is the path on the filesystem to the directory containing your static media. The MEDIA_URL is the URL that makes the static media accessible over HTTP. The docs: http://docs.djangoproject.com/en/1.2/ref/settings/#media-root The main idea is that serving things through python+django is expensive. Since your media is static you don't need to pay

What does it mean for an object to be unscriptable?

早过忘川 提交于 2019-12-09 14:45:54
问题 I don't know what's going on here... I just want to check the value of a model field and then update it accordingly... any help or insight is appreciated! model: class UserProfile(models.Model): user = models.ForeignKey(User, unique=True) beta = models.CharField(max_length=1, blank=True, null=True) view: from internal.accounts.models import UserProfile from django.contrib.auth.models import User @login_required def beta_testers(request): user = User.objects.get(username=request.user.username)

How to get the submitted value of a form in a Django class-based view?

末鹿安然 提交于 2019-12-09 14:38:14
问题 I have a Django form that looks like this: class myForm(forms.Form): email = forms.EmailField( label="Email", max_length=254, required=True, ) I have a an associated Class-Based FormView as shown below. I can see that the form is succesfully validating the data and flow is getting into the form_valid() method below. What I need to know is how to get the value that the user submitted in the email field. form.fields['email'].value doesn't work. class myFormView(FormView): template_name =

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

帅比萌擦擦* 提交于 2019-12-09 06:24:53
问题 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

Add object level permission to generic view

為{幸葍}努か 提交于 2019-12-09 05:36:56
问题 The situation is pretty simple: I'm writing a multi-user blog system. The system should prevent non-owner to edit or delete a blog post. In my view I use generic view. class BlogUpdateView(UpdateView): ... I know I should use @method_decorator to decorate dispatch method. However, most example is just @method_decorator(login_required) or model level permission. How can apply object level permission to check whether request.user is the author of this blog post? For example, I tried to use

Django Templates: Use different css for pages

不打扰是莪最后的温柔 提交于 2019-12-08 21:11:16
问题 New to Django, I want to use different css files for different pages - i.e. page1.css for page1.html, page2.css for page2.html. Is there a way to do this while still extending base.html? In base.html {% load staticfiles %} <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> <title>{% block title %}Default Title{% endblock %}</title> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" /> <!--

Django - using reverse() on Class-based views

半腔热情 提交于 2019-12-08 18:48:33
I have the following urls configuration in my Django project: urlpatterns = patterns('', (r'^my-view$', MyViewClass.as_view()), ) Is there a way to use the reverse() function to get the url of the above view? Yes there is. Use the name argument of the url function to define a name for the url, then you can use reverse on this name: from django.conf.urls import patterns, url urlpatterns = patterns('', url(r'^my-view$', MyViewClass.as_view(), name='my_view'), ) reverse('my_view') 来源: https://stackoverflow.com/questions/25140133/django-using-reverse-on-class-based-views

RetrieveAPIView without lookup field?

狂风中的少年 提交于 2019-12-08 17:43:27
问题 By default RetrieveAPIView or RetrieveUpdateAPIView requires lookup_field to retrieve Model. However in my case, I want to retrieve my model by self.request.user. Here is views.py example class ProfileRetrieveAndUpdateProfile(generics.RetrieveUpdateAPIView): queryset = Profile.objects.all() serializer_class = ProfileRetrieveAndUpdateSerializer lookup_field = 'user_id' def get_queryset(self): qs = Profile.objects.all() logged_in_user_profile = qs.filter(user=self.request.user) return logged_in

Change the name attribute of form field in django template using

坚强是说给别人听的谎言 提交于 2019-12-08 16:55:26
问题 I have form field {{form.item}} which will render to <input type="text" name="item" > How can i change the name attribute of the form field using custom template tags? I tried by sending the form to template tag where form.fields['item'].widget.attrs['name'] = 'new_name' But I'm not getting success. I need to change the name attribute in template. UPDATE models.py class A(models.Model): name = models.CharField(50) type = models.CharField(50) class B(models.Model): field1 = ForeignKeyField(A)