django-class-based-views

Django - How to send a success message using a UpdateView CBV

允我心安 提交于 2020-05-13 04:45:11
问题 (first of all sorry for my bad english) I'm trying to show a message in a UpdateView when the users save the changes! This is my view class NeedUpdateView(UpdateView): model = Need template_name = 'purchases/needs_update_form.html' pk_url_kwarg = 'need_id' success_message = 'List successfully saved!!!!' fields = [ 'detail', ] When you save the app loads the same template! but i like to show a bootstrap alert if save the object! This is code in the template to show the message {% if messages %

How to stop __init__ of a class based view executing twice in django?

让人想犯罪 __ 提交于 2020-04-18 03:44:11
问题 I have shuffled the questions and corresponding options in the exam. Once student has wriiten the exam score will be displayed, and I want to show the student their answer sheet in the same way as they have seen while writing the exam with their answers and correct answers. So I decided to use random.seed(). Why init () is executing twice ? [12/Mar/2020 14:08:58] "GET /exam/4/ HTTP/1.1" 200 11103 [12/Mar/2020 14:41:15] "GET / HTTP/1.1" 200 5059 init seed = 13 student now writing the exam with

Django - DetailView - `get_object` function confusion

半腔热情 提交于 2020-03-04 05:27:49
问题 I'm new to CBV. Not sure why this isn't working... views.py class ItemDetailView(DetailView): '''display an individual item''' model = Item template_name = 'boutique/item.html' context_object_name = 'item' # With model specified, following code would be redundant, wouldn't it?? However... # def get_object(self): # return get_object_or_404(Item, pk=self.kwargs.get('item_pk')) def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) # add categories for navbar link

Django Bounded forms in Class based views - CreateView class

六眼飞鱼酱① 提交于 2020-02-08 02:46:25
问题 How would I go about overriding the GET method in CreateView class based views to generate a bounded form , by which I mean it would have a preset value for some of its fields in the generated form (I need to instantiate the form with some defaults and not use the blank version of the form that CreateView uses by default). I tried looking at https://ccbv.co.uk/projects/Django/1.6/django.views.generic.edit/CreateView/ but dont quite understand the flow of the GET method in this class.

Django: authenticate based on an object's properties using class-based views

只谈情不闲聊 提交于 2020-01-23 14:40:28
问题 Let's say my app is like a forum, but that each post has a group of people which may see it. SecretPost(Model): can_see = myapp.main.models.GroupOfUsers() I want to write a view which restricts users' access to these posts, and I'd prefer to use decorators, since that's how I've been handling access control everywhere else. SecretPostView(DetailView): """Can only be seen by members of its group""" @method_decorator(part_of_its_group) def dispatch(self, request, *args, **kwargs): return super

Django: Add another subclass in a class based view

↘锁芯ラ 提交于 2020-01-17 03:29:25
问题 This is my first django app and I was wondering if it is possible to have a general class which will be extended by all Views. For example class GeneralParent: def __init__(self): #SETTING Up different variables self.LoggedIn = false def isLoggedIn(self): return self.LoggedIn class FirstView(TemplateView): ####other stuff## def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) allLeads = len(self.getAllLeads()) context['isLoggedIn'] = ####CALL

How to get multiple files from a django FileField after model form object has been saved when form.save() does not return object

旧巷老猫 提交于 2020-01-16 08:43:12
问题 I have a django form class that extends django-postman's WriteForm (which is a ModelForm) with an additional field to upload some files. from postman.forms import WriteForm class MyWriteForm(WriteForm): file_field = forms.FileField(widget=forms.ClearableFileInput(attrs={'multiple': True})) However, I need the saved model before I can do anything with the files. If I follow the example in the docs, I can extend postman's FormView and overwrite the save() method: from postman.views import

Saving inlineformset in Django class-based views (CBV)

老子叫甜甜 提交于 2020-01-15 01:26:12
问题 So I'm in the process of working on a web application that has implemented security questions into it's registration process. Because of the way my models are setup and the fact that I am trying to use Django's Class based views (CBV), I've had a bit of problems getting this all to integrate cleanly. Here are what my models look like: Model.py class AcctSecurityQuestions(models.Model): class Meta: db_table = 'security_questions' id = models.AutoField(primary_key=True) question = models

Group serializer results based on value

不羁的心 提交于 2020-01-14 05:37:10
问题 I'm fairly new to Django, so I can't tell if this is possible using a class based view, but I would like to group search results based on a value in the JSON returned from the query. Basically I want my results to go from this: { "id": "0038", "attributes": [ { "name": "State", "values": "CA" }, { "name": "Areas", "values": "Value 1" }, { "name": "Areas", "values": "Value 2" }, { "name": "Areas", "values": "Value 3" }]} To this: {"id": "0038", "attributes": [ { "name": "State Licenses",

How to pass parameters to django generic views

限于喜欢 提交于 2020-01-13 07:16:49
问题 I would like to pass a number to my generic view (DetailView) to get one object Here is my code Urlpattern (r'^newreportview/(?P<number>\w+)/$', NewReportView.as_view()), View Class class NewReportView(DetailView): template_name = "report/newreportview.html" context_object_name = "newreportview" def get_queryset(self): task= get_object_or_404(MyTask,applicationnnumber=self.args[0]) return task I guess something is wrong in this line name = get_object_or_404(MyTask,applicationnnumber=self.args