django-templates

Present data from python dictionary to django template.?

☆樱花仙子☆ 提交于 2019-12-03 09:12:13
问题 I have a dictionary data = {'sok': [ [1, 10] ], 'sao': [ [1, 10] ],'sok&sao':[ [2,20]] } How Can I (Loop trough Dictionary ) present My data as (HTML) table to Django template.?? This format that as table author qty Amount sok 1 10 sao 1 10 sok&sao 2 20 total 回答1: You can use the dict.items() method to get the dictionary elements: <table> <tr> <td>author</td> <td>qty</td> <td>Amount</td> </tr> {% for author, values in data.items %} <tr> <td>{{author}}</td> {% for v in values.0 %} <td>{{v}}<

Why and When to use Django mark_safe() function

我怕爱的太早我们不能终老 提交于 2019-12-03 09:06:47
问题 After reading the document, the mark_safe() still seems a myth. I guess it is related to CSRF stuff. But why and when shall the mark_safe() be used? Here is the documentation mark_safe(s)[source]¶ Explicitly mark a string as safe for (HTML) output purposes. The returned object can be used everywhere a string or unicode object is appropriate. Can be called multiple times on a single string. For building up fragments of HTML, you should normally be using django.utils.html.format_html() instead.

Django-Admin: integrating custom forms

好久不见. 提交于 2019-12-03 08:56:49
I am trying to build a import form for a CSV file into the admin interface for a given model. the view can be reached from the change_list of the model, but it throws a template syntax error. What am I doing wrong? Do I have to create my own template or can I re-use the existing admin/change_form.html somehow and I just don't get it? Traceback Line 60 is highlighted. Template error: In template site-packages\django\contrib\admin\templates\admin\change_form.html, error at line 60 Caught KeyError while rendering: 'opts' 50 : {% endfor %} 51 : 52 : {% block after_field_sets %}{% endblock %} 53 :

django & the “TemplateDoesNotExist” error

安稳与你 提交于 2019-12-03 08:52:37
问题 Ive got an as it seems common beginners problem. im working on my first django project and when I set up my view I get an "TemplateDoesNotExist" error. Ive spend lots of hours on this now - and I know theres lots of topics on it but nothing helped me till now. I hope I can supply all the information needed so an advanced django user can probably directly see what Im doing wrong. im using the developement server. and windows 7 & sqlite3. this is the error I get: TemplateDoesNotExist at /skates

integrating third-party django apps and templates

大憨熊 提交于 2019-12-03 08:48:24
I'm new to Django and fairly confused about how third-party apps are best integrated. In my (possibly naive) interpretation of DRY, I want to minimize copy/pasting not only my own code but other people's code as well, so I'm happy with, for example, the pattern of using contrib.auth as mostly a black box if I needed additional info on auth.User (by extending auth.User with a UserProfile object or inheritance). And this was how I imagined I'd use most third-party apps as well. However, I quickly found out this was very difficult, so now I've resigned to having "copies" of all of my third-party

How to use two different Django Form at the same template?

雨燕双飞 提交于 2019-12-03 07:59:08
My forms.py: class AlertForm(forms.ModelForm): class Meta: model=Alert fields = ('high','medium', 'user') widgets = { 'user': forms.HiddenInput() } AlertCountFormset = modelformset_factory(Alert, form = AlertForm) Another Django Form class: class NotifierForm(forms.ModelForm): high = forms.ChoiceField(choices=NOTIFIER_TYPE) medium = forms.ChoiceField(choices=NOTIFIER_TYPE) low = forms.ChoiceField(choices=NOTIFIER_TYPE) def save(self, commit=True): alert = super(NotifierForm, self).save(commit=False) alert.high = self.cleaned_data["high"] alert.medium = self.cleaned_data["medium"] alert.low =

Auto load content on scroll down

雨燕双飞 提交于 2019-12-03 07:35:07
I am creating a blog on django/webfaction. My hompage currently displays 4 posts by default (posts are limited to 4 on queryset in urls.py ). Now I would like to load four more posts once user reaches the bottom of the page and keep on continuing that until last post is reached. How to achieve this? Anoop If you want to load your content on reaching extreme bottom of document use following code: $(window).scroll(function() { if($(window).scrollTop() == $(document).height() - $(window).height()) { // load your content } }); If you want to load content before reaching 100 pixel of bottom use var

How can I use Django Social Auth to connect with Twitter?

青春壹個敷衍的年華 提交于 2019-12-03 07:26:40
问题 I'm trying to use the Django Social Auth package to connect with Twitter, but I'm having difficulty understanding how exactly to do this as I can't find any examples. I am assuming that Django Social Auth is the best package to use for this purpose. I've looked at a few examples that use Facebook, and from this have added the following to my settings.py file: AUTHENTICATION_BACKENDS = ( 'social_auth.backends.twitter.TwitterBackend', 'django.contrib.auth.backends.ModelBackend', ) # overwriting

How to concatenate a string to a number inside a template tag in Django

血红的双手。 提交于 2019-12-03 06:57:27
I've found a similar question on StackOverflow , but the solution doesn't seem to work for me, unless I'm doing it wrong. I have a ID number, which I'd like to append to a string in a template tag. Here's my attempt: {% with "image-"|add:vid.the_id as image_id %} {# custom template tag to generate image #} {% image vid.teaser_thumbnail alt=vid.title id=image_id %} {% endwith %} But image_id is coming out as empty. What am I doing wrong here? My desired output of image_id would be something like "image-8989723123". Try this way (added with statement with stringformat on top of yours): {% with

How to redirect users to a specific url after registration in django registration?

Deadly 提交于 2019-12-03 06:54:18
So I am using django-registration app to implement a user registration page for my site. I used Django's backends.simple views which allows the users to immediately login after registration. My question is how do I redirect them to my other app's page located in the same directory as the project. Here is what my main urls.py looks like: from django.conf.urls import patterns, include, url # Uncomment the next two lines to enable the admin: # from django.contrib import admin # admin.autodiscover() urlpatterns = patterns('', url(r'^accounts/', include('registration.backends.simple.urls')), url(r'