django-templates

Django add class to form <input ..> field

99封情书 提交于 2019-12-06 01:51:52
问题 We are looking for a solution to add a CSS class attribute to a Django form's <input..> field. We've seen the advice that suggests we wrap the field in a <div> http://docs.djangoproject.com/en/1.2/topics/forms/#customizing-the-form-template, but this advice mainly seems to apply to a fields label, not it's <input ...> . This particular advice doesn't work if you are trying to create a border around the <input> field. In that case, the <div> will be applied to the bounding box, and not the

I can't understand the django-markdownx's usage

人盡茶涼 提交于 2019-12-06 01:20:11
问题 It is kind of embarrassing. But I can't seems to understand django-markdownx's documentation on how to use the app. I've followed the Getting Started guide, installed the app and dependencies, added jquery and it works in the Admin backend. But the template doesn't render text = MarkdownxField() as correctly formatted markdown, but as plain text. I don't understand the part ...and then, include a form's required media in the template using {{ form.media }} : <form method="POST" action="">{%

Passing request.user to template in Django

一个人想着一个人 提交于 2019-12-06 01:15:42
Is there another way to get the request.user by not passing it from the views? I think passing request.user from all functions in views to the template is quite wrong. Is there any method or way that the template will get the user or any object in the database? By default (I am talking about Django version 1.3) you do not need change TEMPLATE_CONTEXT_PROCESSORS. Because default value already contains *django.contrib.auth.context_processors.auth*. So to your question: By default, you should be able to use user , messages and perms variables in your template. For example: User: {{user.username}}

Optimize code in Django - view ManyToMany as matrix

烂漫一生 提交于 2019-12-06 00:48:33
I'm trying to show user group permissions in Django and show them in a "Drupal" style like a matrix. It works, but it takes too long to make the query and paint it in the template. Is there some way to improve my code? view img up(accomplished),down(views and template.html) views : def GroupPermissionsView(request): title = "Groups Permissions" groups = Group.objects.all() permissions = Permission.objects.all() context = Context({ 'title': title, 'groups': groups, 'permissions': permissions, }) return render( request, 'forms_permissions.html', context ) template: <table class="table table

Django async send notifications

故事扮演 提交于 2019-12-06 00:44:55
I have a notifications for a users when the admin add notification to user i want to display on user's home page like a facebook likes or activity feed is it possible with django ? models.py class Notification(BaseModel): created_user = models.ForeignKey(User) title = models.CharField(max_length=225) description = models.TextField(max_length=600) is_read = models.BooleanField(default=False) priority = models.CharField(max_length=20, choices=NOTIFICATION_STATUS, default=LOW) def __str__(self): return self.title i'm able to add a new notificiation but the notifications listing when the user

How to make a Template extend another one?

江枫思渺然 提交于 2019-12-06 00:41:50
I've got some templates which I'm building from some data I've got stored in my DB: my_template = Template(string_data) Now I want that template to extend another one (also stored as text in the DB). How can I do that? Looking for something like: my_template.base = other_template Or whatever the syntax is. Can't find documentation on this. I see template.nodelist in the source; can I maybe prepend some kind of extends node? Added a bounty... will grant to first person who can tell me how to do this without hacking the core of django. No need to hack the core of Django. Creating your own

Load Django Form Object with data from Model instance already “loaded”

China☆狼群 提交于 2019-12-06 00:29:37
I have the following type of template in Django: <form .... > <label class="control-label" for="id_quantity">Quantiy</label> {{ form.quantity }} <label class="control-label" for="id_title">Quantiy</label> {{ form.title}} </form> The view contains a ModelForm object in its context from which the "form" variable in the template is derived. Often, you can pass a form that is bound to data like request.POST. How do I load a form that is bound to the data of a specific instance of a model? Is there any special syntax? There are two options here: MyModelForm(instance=myInstanceModel) MyModelForm

how to get checkbox value in django?

喜夏-厌秋 提交于 2019-12-05 22:31:58
<tr name="item"> <td><input type="checkbox" ></td> <td>>{{ foo }}</td> <td> {{ bar }} </td> </tr> Now I want that in django views request.POST.getlist('item') returns the value of ie foo and bar . But it is returning null. I'm not sure to understand everything you asked, but if you want to get "foo" and "bar" when the user submit the form, you will have to add them in a form element like hidden or textfields (depending on if the user can modify them or not). The server will never receive the whole DOM when you submit a form. Also, you will have to find a way to indicate on which checkbox

Django: Can't override admin templates when they are already overridden?

元气小坏坏 提交于 2019-12-05 21:37:35
To add some content to a Django admin view of a model, I want to override the change_form.html template. According to the documentation I would need to create a file change_form.html in a the folder /project-path/templates/admin/appname/modelname/ . Of course I need to make sure, that this path is also available in TEMPLATE_DIRS . Such a file could look like this: {% extends "admin/change_form.html" %} {% load i18n %} {% block after_field_sets %} SOME CONTENT {% endblock %} However, I make use of django-guardian to have object permissions. This Django app overrides change_form.html as well

Use field label as placeholder with django-widget-tweaks

喜夏-厌秋 提交于 2019-12-05 21:34:11
I am using django-widget-tweaks and unable to figure out how to add a field variable as placeholder , like the following: <div class="col-sm-10"> {{ field|append_attr:"class:form-control"|append_attr:"placeholder:field.label" }} {% if field.help_text %} <p class="help-block"><small>{{ field.help_text }}</small></p> {% endif %} </div> field.label above does not evaluate and puts the string "field.label" as the placeholder on the page. Some SO posts suggest registering a custom tag/filter which seems complicated for something this simple. I am now using render_field to render the field instead