django request in template
settings.py: TEMPLATE_CONTEXT_PROCESSORS = ( # … ‘django.core.context_processors.request’, # … )
settings.py: TEMPLATE_CONTEXT_PROCESSORS = ( # … ‘django.core.context_processors.request’, # … )
You can subclass Django’s AdminSite (put it eg. in admin.py in your project root): from django.contrib.admin.sites import AdminSite class MyAdminSite(AdminSite): pass #or overwrite some methods for different functionality myadmin = MyAdminSite(name=”myadmin”) At least from 1.9 on you need to add the name parameter to make it work properly. This is used to create the revers … Read more
Django < 2.0 Answer: Use Django’s Request object (which you have access to) to retrieve the request.path_info, then retrieve the PK from the args in the resolve match. Example: from django.contrib import admin from django.core.urlresolvers import resolve from app.models import YourParentModel, YourInlineModel class YourInlineModelInline(admin.StackedInline): model = YourInlineModel def get_parent_object_from_request(self, request): “”” Returns the parent object … Read more
Here is a solution to the problem. Thanks to koniiiik i tried to solve this by extending the *get_fieldsets* method class ProductAdmin(admin.ModelAdmin): def get_fieldsets(self, request, obj=None): fieldsets = super(ProductAdmin, self).get_fieldsets(request, obj) fieldsets[0][1][‘fields’] += [‘foo’] return fieldsets If you use multiple fieldsets be sure to add the to the right fieldset by using the appropriate index.
One approach is to use chain of filters: node_query = Node.objects.all() pk_list = [10, 6, 3] for pk in pk_list: node_query = node_query.filter(tags=pk) Now node_query will match node, that has at least three tags with pk 10, 6, 3. To exact matching of three tags: UPDATE: Thanks to @janos and @Adrián López, the correct answer … Read more
As of Django 1.2, if your form has an attribute named required_css_class, it will be added to BoundField.css_classes for required fields. You can then use CSS to style the required parts of the form as desired. A typical use case: # views.py class MyForm(django.forms.Form): required_css_class=”required” … … /* CSS */ th.required { font-weight: bold; } … Read more
From here This is a feature, not a bug. If you want a list of values for a key, use the following: values = request.POST.getlist(‘key’) And this should help retrieving list items from request.POST in django/python The function below converts a QueryDict object to a python dictionary. It’s a slight modification of Django’s QueryDict.dict() method. … Read more
Create a validation method like: def validate_file_extension(value): if not value.name.endswith(‘.pdf’): raise ValidationError(u’Error message’) and include it on the FileField validators like this: actual_file = models.FileField(upload_to=’uploaded_files’, validators=[validate_file_extension]) Also, instead of manually setting which extensions your model allows, you should create a list on your setting.py and iterate over it. Edit To filter for multiple files: def … Read more
To expand on Yuji’s answer, here are some specifics on overriding change_list_results.html … Override changelist_view as described above in step 1, and also described here at djangoproject. Or auto-override by placing in the appropriate directory as in step 2 above. (Note that the step 2 path shown above is model-specific. App-specific would be /admin/<MyAppName>/change_list.html under … Read more
You have to create second form for PrinterAddress and handle both forms in you view: if all((profile_form.is_valid(), address_form.is_valid())): profile = profile_form.save() address = address_form.save(commit=False) address.printer_profile = profile address.save() Of course in the template you need to show both forms under one <form> tag 🙂 <form action=”” method=”post”> {% csrf_token %} {{ profile_form }} {{ address_form … Read more