django-views

django - 2 views in one template

陌路散爱 提交于 2019-12-04 07:40:46
i have 2 different views which are getting filtered data from db. and i have to use these views in one template file(admin.html) but i cant use multiple views on a page at same time. here is my view1: draft_list = Post.objects.filter(isdraft=True).order_by("-posted") return render_to_response('userside/admin.html', {'draft_list':draft_list,}, context_instance = RequestContext(request)) view2 : publish_list = Post.objects.filter(isdraft=False).order_by("-posted") return render_to_response('userside/admin.html', {'publish_list':publish_list,}, context_instance = RequestContext(request)) i d like

Django REST Framework: difference between views and viewsets?

半城伤御伤魂 提交于 2019-12-04 07:30:20
问题 May be relevant. What is the difference between views and viewsets ? And what about router and urlpatterns ? 回答1: ViewSets and Routers are simple tools to speed-up implementing of your API, if you're aiming to standard behaviour and standard URLs. Using ViewSet you don't have to create separate views for getting list of objects and detail of one object. ViewSet will handle for you in consistent way both list and detail. Using Router will connect your ViewSet into "standarized" (it's not

Converting a nested dictionary to a list

我们两清 提交于 2019-12-04 07:19:10
I know there are many dict to list questions on here but I can't quite find the information I need for my situation so I'm asking a new quetion. Some background: I'm using a hierarchical package for my models and the built-in function which generates the tree structure outputs a nested loop to indicate parents, children, etc. My goal is to keep the logic in views and output a list so that I can simply loop over it in my templates. Here is my data, in the tree structure: 1 -1.1 --1.1.1 ---1.1.1.1 --1.1.2 -1.2 --1.2.1 --1.2.2 -1.3 Here is the nested dictionary I am getting as a result { <Part: 1

django file upload from json

北战南征 提交于 2019-12-04 06:35:35
问题 Hi i have given the complete code for my file upload .But in my views i am just trying to return a response and get the alert on the UI.But none of the return statements are working.How to rectify this.. EDIT : i see the logging statements on the server side <h1>Add Content</h1> <form method="post" enctype="multipart/form-data" id="contentform" action="/content/savecontent/" >{% csrf_token %} <b> <table> <tr><td><font>*</font>Content Name</td><td> <input type="text" id="cname" name="cname"

How do you access models from other installed apps in Django when in the same subdirectory?

痞子三分冷 提交于 2019-12-04 06:19:24
问题 I have the following structure for my project: myproject/ |-- myproject/ | |-- __init__.py | |-- settings.py | |-- urls.py | |-- wsgi.py |-- apps/ | |-- dashboard/ | | |-- static/ | | |-- templates/ | | |-- __init__.py | | |-- admin.py | | |-- apps.py | | |-- forms.py | | |-- models.py | | |-- tests.py | | |-- views.py | |-- data/ | | |-- __init__.py | | |-- apps.py | | |-- models.py | | |-- router.py | | |-- tests.py | |-- __init__.py |-- manage.py The settings file has these installed apps

invalid literal for int() with base 10: 'on' Python-Django

ⅰ亾dé卋堺 提交于 2019-12-04 05:04:13
i am learning django from official django tutorial. and i am getting this error when vote something from form. this caused from - probably - vote function under views.py here is my views.py / vote function : def vote(request,poll_id): p=get_object_or_404(Poll, pk=poll_id) try: selected_choice = p.choice_set.get(pk=request.POST['choice']) except (KeyError, Choice.DoesNotExist): return render_to_response('polls/detail.html', {'poll':p, 'error_message' : "didint select anything ",}, context_instance= RequestContext(request)) else: selected_choice.votes += 1 selected_choice.save() return

Pass request.user to view without altering the url

本秂侑毒 提交于 2019-12-04 04:47:48
问题 I'm trying to write a view where the current logged in user's information is retrieved. My view is written as below, and it works perfectly fine as long as I pass the user to the view in the URL. def index(request, username, template="index.html"): user = get_object_or_404(User, username__iexact=username) expression_qs = Expression.objects.all().order_by('-created') album_qs = Album.objects.all().filter(head__isnull=False, is_public=True).order_by('-created') user_following = user

Django templates syntax error

一个人想着一个人 提交于 2019-12-04 03:48:50
问题 Is there any problem with the syntax in the following code, there is a error as Invalid block tag: 'else' {% ifequal chat_profile 1 %} {% extends "chatprofile/chat_profile1.html" %} {% else %} {% extends "chatprofile/chat_profile.html" %} {% endifequal %} 回答1: The documentation states: If you use {% extends %} in a template, it must be the first template tag in that template. Template inheritance won't work, otherwise. So consider using a design where you can use {% include %} instead. 回答2:

Sorting objects in template

大城市里の小女人 提交于 2019-12-04 03:47:27
问题 Let these models: class Category(models.Model): name = models.CharField(max_length=20) class Word(models.Model): name = models.CharField(max_length=200) votes = models.IntegerField(default=1) categories = models.ManyToManyField(Category, null=True, blank=True) this view: def main_page(request): words = Word.objects.all() categories = Category.objects.all() return render(request, "main_page.html", {'words': words}) and this template: {% for category in categories %} {% for word in category

How to raise a error inside form_valid method of a CreateView

我与影子孤独终老i 提交于 2019-12-04 03:31:20
In a Django project, I have a view (cloud), type: CreateView. This view has a inlineformset_factory. It works. But, if i submit the form with a error (look at "messages.error" below), the page is redirected to project.get_absolute_url(). The problem is: the form content back empty. I know thats because the HttpResponseRedirect. How can I change this without to break the form? views.py class cloud(CreateView): template_name = 'base/cloud.html' form_class = UserForm def get_context_data(self, **kwargs): context = super(cloud, self).get_context_data(**kwargs) project = get_object_or_404(Project,