django-views

Django Master-Detail View Plugins

谁都会走 提交于 2019-12-01 11:26:51
Let's say I have 3 django apps, app Country , app Social and app Financial . Country is a 'master navigation' app. It lists all the countries in a 'index' view and shows details for each country on its 'details' view. Each country's details include their Social details (from the social app) and their Financial details (from the financial app). Social and Financial both have a detail view (for each country) Is there an elegant way to 'plug' in those sub-detail views into the master detail view provided by Countries? So for each country detail page I would see 2 tabs showing the social and the

Queryset for current logged in user Django

◇◆丶佛笑我妖孽 提交于 2019-12-01 11:18:11
I am doing queryset with my model. Now the queryset is displaying all the data in my html page. But I want to display only the logged in users data. models.py class Data(models.Model): user = models.OneToOneField(User, on_delete=models.CASCADE) Status = models.CharField(max_length=10, blank=False) Date = models.DateField(blank=True, null=True) views.py @login_required def search(request): status_list = Data.objects.all() status_filter = UserFilter(request.GET, queryset=status_list) return render(request, 'users/data.html', {'filter': status_filter}) filters.py class UserFilter(django_filters

Django: Redirect to the previous page and keep the scroll position

萝らか妹 提交于 2019-12-01 11:15:34
I have seen this question where you can redirect to the previous page using: return HttpResponseRedirect(request.META.get('HTTP_REFERER')) but is there a way to also keep the scroll position that it was at? The above reloads the page afresh. After some back and forth, this is what I came up with (with help from this answer): from django.http import HttpResponse def myview (request): .. .. return HttpResponse('<script>history.back();</script>') Also, keep in mind that history.back() does not 'reload' the page. To also reload the page, we have to use location.reload() which also keeps the scroll

Understanding django.shortcuts.redirect

不打扰是莪最后的温柔 提交于 2019-12-01 11:14:24
I have a couple problems understanding how redirect or rather reverse really work. In the main urls.py I have: from django.conf.urls import patterns, include, url from django.views.generic.simple import redirect_to urlpatterns = patterns('', url(r'^$', redirect_to, {'url': '/monitor/'}), url(r'^monitor/', include('monitor.urls')), ) and in monitors.urls I have: from django.conf.urls import patterns, include, url urlpatterns = patterns('monitor.views', (r'^$', 'index'), (r'^abc/(?P<id>.*$)', 'abc'), ) When you call /monitor I want to redirect it to /monitor/abc so I did: def index(request):

Understanding django.shortcuts.redirect

泄露秘密 提交于 2019-12-01 09:54:20
问题 I have a couple problems understanding how redirect or rather reverse really work. In the main urls.py I have: from django.conf.urls import patterns, include, url from django.views.generic.simple import redirect_to urlpatterns = patterns('', url(r'^$', redirect_to, {'url': '/monitor/'}), url(r'^monitor/', include('monitor.urls')), ) and in monitors.urls I have: from django.conf.urls import patterns, include, url urlpatterns = patterns('monitor.views', (r'^$', 'index'), (r'^abc/(?P<id>.*$)',

My defaultdict(list) won't show up on template but does in my view [duplicate]

老子叫甜甜 提交于 2019-12-01 09:22:47
Possible Duplicate: Django template can’t loop defaultdict I am wondering why my defaultdict(list) will show when I test it in my views.py but when I go to show the data on my template, I get nothing, not even an error. Any suggestions? Here is my views.py - confirm_list is my defaultdict(list) def confirmations_report(request, *args, **kwargs): from investments.models import Investment, InvestmentManager from reports.forms import ConfirmationsForm from collections import defaultdict import ho.pisa as pisa import cStringIO as StringIO import os.path confirm_list = defaultdict(list) context = {

Multiple models generic DetailView to template

一曲冷凌霜 提交于 2019-12-01 08:37:14
I have 2 models and I got the IndexView working properly using the get_context_data method. However my DetailView using the same technique is not working. How do I simply get 2 models into the DetailView ? views.py from .models import CharacterSeries, CharacterUniverse class IndexView(generic.ListView): template_name = 'character/index.html' context_object_name = 'character_series_list' def get_queryset(self): return CharacterSeries.objects.order_by('name') def get_context_data(self, **kwargs): context = super(IndexView, self).get_context_data(**kwargs) context['character_universe_list'] =

django remove source files and generate pyc files

可紊 提交于 2019-12-01 07:48:44
问题 I want to remove all .py files in my django project.But pyc files are not generated as yet.. What is the settings that needs to be changed to generate the .pyc files 回答1: compileall can be used to compile all Python scripts in the project directory. python -m compileall path/to/project 来源: https://stackoverflow.com/questions/4311265/django-remove-source-files-and-generate-pyc-files

Passing a python list to django template

▼魔方 西西 提交于 2019-12-01 07:40:15
I want to display a list of things on my template. So I have a view to generate that list and pass it to template like this: newlinks = [] try: links=urllib2.urlopen("<<Some HTML file link>>").readlines() except (urllib2.HTTPError): links = '' pass for link in links: newlinks.append(link[0:-1]) return render_to_response('template11.html', {'links',newlinks}, context_instance=RequestContext(request)) But while rendering it, i get TypeError Exception Type: TypeError Exception Value: unhashable type: 'list' This is template code: {% for link in links %} <li>{{ link }}</li> {% endfor %} I don't

Django filter exclude foreign key

半腔热情 提交于 2019-12-01 07:07:57
问题 I'm coding a news website which has two models News and BestNews.News is a foreign key of BestNews.News stands for all the news,Best News is selected to be recommended news. Now I have rendered News list and Best News list in index.html.But some news in these two parts are duplicated. I hope news that in Best News list, will not appear in News list, and once I have removed the news from the Best News in admin, the news which has been removed from best news will appear News list. Here is my