django-class-based-views

When to override get method in Django CBV?

回眸只為那壹抹淺笑 提交于 2020-01-11 13:50:10
问题 I've been learning Django and one source of confusion I have is with class based views and when to override the get method. I've looked through the documentation and it explains what get does but it doesn't explain when I should override get. I originally created a view this way: class ExampleView(generic.ListView): template_name = 'ppm/ppm.html' paginate_by = 5 def get(self, request): profiles_set = EmployeeProfile.objects.all() context = { 'profiles_set': profiles_set, 'title': 'Employee

Provide extra context to all views

半腔热情 提交于 2020-01-10 14:11:59
问题 I'm putting together a project management website for my team using django. My base template includes a sidebar menu which contains a list of all projects and users, linking to a DetailView for that user or project, respectively. My problem is that I need to provide the User and Project models to every view so that I can render that sidebar. I know how to add extra context; the problem is that I feel like I'm violating DRY by modifying the context at each level. Is it possible to simply

DRF - access request's POST data

…衆ロ難τιáo~ 提交于 2020-01-07 09:03:24
问题 I do a request to my local server using postman like this: As you can see it's a post request. In my view ( APIView ) I need access to the json data. But when I try: request.POST # <QueryDict: {}> or request.data # ¿? # AttributeError: 'WSGIRequest' object has no attribute 'data' The only way I can see the sent data is when I access to request.body # '{\n "token": "6J3qG4Ji2Jw44eIklKvPYxUgclfGRWHZDKG",\n "city": "Port Orange",\n "state": "FL",\n "formatted_address": "Peach Blossom Blvd 5329",

Update in three models at the same time in form_valid django

…衆ロ難τιáo~ 提交于 2020-01-06 21:07:18
问题 I have three models, Propietario, Administrador and Encargado. And I have three forms with ModelForm calling each of these models. The forms may to have three options depends of the user does: If user picked option A, will display three forms where can fill different data for each form If user picked option B, will display two forms, where the data filled in the form FormPropietario will save in Propietario model and automatically in Administrator model the same data. And the second form

multiple pagination in django class based view

笑着哭i 提交于 2020-01-06 15:24:51
问题 The code below paginates the queryset but how can I paginate context['guser1'] class AuthorList(ListView): template_name = 'authorList.html' paginate_by = 10 queryset = Author.objects.order_by('date') def get_context_data(self, **kwargs): context = super(AuthorList, self).get_context_data(**kwargs) if int(self.kwargs['user_id']) != self.request.user.id: raise PermissionDenied if self.request.user.username == 'guest': raise PermissionDenied context['guser1'] = Author.objects.order_by('date')

Django: TemplateView or FormView?

纵然是瞬间 提交于 2020-01-06 14:26:32
问题 I am currently trying to bring my function-based view into a class-based view. The question I am currently struggling with is if I should either trying to move it into a TemplateView with FormMixin or in a FormView with ContextMixin. Do you have any tips on how to decide what's best? def event_detail_view(request, event, organizer): queryset = Event.objects.filter(organizer__slug=organizer) event = get_object_or_404(queryset, slug=event) tickets = collect_all_tickets(event, organizer)

django class based views for all categories with all entires

☆樱花仙子☆ 提交于 2020-01-06 08:46:10
问题 I have tried this for a couple of hours, and looked at alot of documentation, but I cant get it right. I dont think I'll see a solution to this anytime soon, so maybe someone could see what is wrong? I want a view to show all my categories and all my connected entries to those categories. I have tried to follow this example: django class-based-views topic But I get this error: tuple index out of range My model: STATUS_CHOICES = ( ('d', 'Draft'), ('p', 'Published'), ('w', 'Whitdrawn'), ) class

Django Createview default value for a foreign key field

ε祈祈猫儿з 提交于 2020-01-04 09:39:19
问题 I have two classes(tables) schools and students that are related together(foreignkey) When I create a new student I want it to autofill the school field (which is a foreignkey to name field in School class(table) because it is already linked to a school. I have tried def get_initial(self): with no luck (it give me error). If anyone can point out what I am doing wrong…thanks Models.py: class School(models.Model): school_pk = models.AutoField(primary_key=True) name = models.CharField(max_length

Django: how to set content-type header to text/xml within a class-based view?

独自空忆成欢 提交于 2020-01-04 06:29:12
问题 I'm trying to do it this way, but it doesn't work. class MyView(View): def options(self, request, *args, **kwargs): """ Handles responding to requests for the OPTIONS HTTP verb. """ response = http.HttpResponse() if self.kwargs.has_key('xml'): response['Content-Type'] = 'text/xml; charset=utf-8' return response 回答1: I think the key point is render_to_response in django.views.generic.base , whose code is this: def render_to_response(self, context, **response_kwargs): """ Returns a response,

Django CreateView: set user before validation

删除回忆录丶 提交于 2020-01-02 10:02:05
问题 I have a model that uses different validation for its name field depending on whether the object was created by a user or by the system. class Symbol(models.Model): name = models.CharField(_('name'), unique=True, max_length=64) creator = models.ForeignKey('User', null=True, on_delete=models.CASCADE) def is_system_internal(self): """ whether or not this Symbol belongs to the system rather than having been created by a user """ return (self.creator is None) def clean(self): """ ensure that the