django-views

Django: a class based view with mixins and dispatch method

£可爱£侵袭症+ 提交于 2019-11-29 00:10:23
问题 Normally, I use a dispatch method of a class based view to set some initial variables or add some logic based on user's permissions. For example, from django.views.generic import FormView from braces.views import LoginRequiredMixin class GenerateReportView(LoginRequiredMixin, FormView): template_name = 'reporting/reporting_form.html' form_class = ReportForm def get_form(self, form_class): form = form_class(**self.get_form_kwargs()) if not self.request.user.is_superuser: form.fields['report

Getting next and previous objects in Django

可紊 提交于 2019-11-28 22:43:36
I'm trying to get the next and previous objects of a comic book issue. Simply changing the id number or filtering through date added is not going to work because I don't add the issues sequentially. This is how my views are setup and it WORKS for prev_issue and does return the previous object, but it returns the last object for next_issue and I do not know why. def issue(request, issue_id): issue = get_object_or_404(Issue, pk=issue_id) title = Title.objects.filter(issue=issue) prev_issue = Issue.objects.filter(title=title).filter(number__lt=issue.number)[0:1] next_issue = Issue.objects.filter

Django models avoid duplicates

本秂侑毒 提交于 2019-11-28 21:59:58
In models: class Getdata(models.Model): title = models.CharField(max_length=255) state = models.CharField(max_length=2, choices=STATE, default="0") name = models.ForeignKey(School) created_by = models.ForeignKey(profile) def __unicode__(self): return self.id() In templates: <form> <input type="submit" value="save the data" /> </form> If the user clicks on the save button and the above data is saved in the table, how to avoid the duplicates, i.e. if the user again clicks on the same submit button there should not be another entry for the same values. Or is it something that has to be handled in

django accessing raw many to many created table fields

混江龙づ霸主 提交于 2019-11-28 21:44:00
Model: class Subjects (models.Model): name = models.CharField(max_length=100) places = models.CharField(max_length=100) class Student (models.Model): name = models.CharField(max_length=40) lastname = models.CharField(max_length=80) subjects = models.ManyToManyField(Subjects, blank=True) Django creates appname_student_subjects when I use model above. appname_student_subjects table looks for example, like this: id | student_id | subjects_id ----------------------------------------- 1 | 1 | 10 2 | 4 | 11 3 | 4 | 19 4 | 5 | 10 ... ~1000 How can I access subjects_id field and count how many times

Add data to ModelForm object before saving

痞子三分冷 提交于 2019-11-28 21:12:30
Say I have a form that looks like this: forms.py class CreateASomethingForm(ModelForm): class Meta: model = Something fields = ['field2', 'field3', 'field4'] I want the form to have these three fields. However my Something class also has field1 . My question is - how do I add data to field1 , if I am not using the ModelForm to collect the data. I tried doing something like this, but it isn't working and I am unsure on the proper way to solve this: views.py def create_something_view(request): if (request.method == 'POST'): # Create an object of the form based on POST data obj = CreateASomething

Django class based views - UpdateView with two model forms - one submit

狂风中的少年 提交于 2019-11-28 20:55:57
I have a page with a list of users, and would like to be able to click a link to update their profile. When 'update' is clicked, I should be able to edit the username, first name, ... email, phone number, department etc, in a single page, using a single submit button. I accomplished this by using two forms, one for User, and one for the extra information. The ListView, DeleteView and CreateView work perfectly with these two forms, but not the UpdateView. I am not able to instantiate the two forms with initial data. The question is: how do I instantiate the two forms with data? Overwrite self

django - get() returned more than one topic

巧了我就是萌 提交于 2019-11-28 20:04:05
When I tried to relate an attribute with another one which has an M to M relation I received this error: get() returned more than one topic -- it returned 2! Can you guys tell me what that means and maybe tell me in advance how to avoid this error ? models class LearningObjective(models.Model): learning_objective=models.TextField() class Topic(models.Model): learning_objective_topic=models.ManyToManyField(LearningObjective) topic=models.TextField() output of LearningObjective.objects.all() [<LearningObjective: lO1>, <LearningObjective: lO2>, <LearningObjective: lO3>] output of Topic.objects

how to get a list of all views in a django application?

浪子不回头ぞ 提交于 2019-11-28 19:54:55
Is there any way to get a list of all views in an django app? I have googled for answer. All answers shows a way to get list of urls. Getting list of all the views of a Django project: To get all the views present in a Django project, we create a function get_all_view_names() which takes urlpatterns as input and returns the complete list of views being used in the project as the output. First, we import the root_urlconf module using settings.ROOT_URLCONF . Then root_urlconf.urls.urlpatterns will give us the list of project's urlpatterns. The above urlpatterns list contains RegexURLPattern and

Django form with unknown number of checkbox fields and multiple actions

蹲街弑〆低调 提交于 2019-11-28 18:53:53
I need help with form which looks like a Gmail inbox and have multiple actions. There is a list of items and I want to wrap it with form, on the way that every item have checkbox in the front of the line. So when user select few items he is able to click on two buttons with different actions for example delete and mark read. <form action=""> {% for item in object_list %} <input type="checkbox" id="item.id"> {{ item.name }} {% endfor %} <button type="submit" name="delete">Delete</button> <button type="submit" name="mark_read">Mark read</button> </form> I can find which submit button is user

Django internationalization language codes [closed]

你。 提交于 2019-11-28 18:39:01
Where can I find list of languages and language_code like this. (Swedish,sv) (English,en) Wiki: http://en.wikipedia.org/wiki/List_of_ISO_639-1_codes Thomas If you want something you can use from within django, try: from django.conf import settings this will be in the format above, making it perfect for assignment in one of your models choices= fields. (i.e. user_language = models.CharField(max_length=7, choices=settings.LANGUAGES) ) LANGUAGES = ( ('ar', gettext_noop('Arabic')), ('bg', gettext_noop('Bulgarian')), ('bn', gettext_noop('Bengali')), etc.... ) Note about using settings: Note that