two views in one url

41 views
Skip to first unread message

cha

unread,
Sep 11, 2011, 1:30:24 PM9/11/11
to Django users
Hello all I have two views in two app and I want to get them in main
url file how I Can do that ?

first view in project/app_news/views.py

def article_index(request):
return render_to_response('news/index.html', {
'news_slide': Article.objects.filter(status=1,
statusslide=1)[:6],
'section_list' : Section.objects.all(),
'last_lib' : Library.objects.all()[:3],
},
context_instance=RequestContext(request)
)

secend view in project/app_poll/views.py

def questionlast(request):
try:
question = Question.objects.order_by('id').reverse()[0]
except ObjectDoesNotExist, e:
raise Http404
if request.method == 'POST':
try:
last_choice_id = request.session[question.id]
last_choice = Choice.objects.get(id = last_choice_id)
last_choice.total_votes -= 1
last_choice.save()
except KeyError, e:
pass
choice_id = int(request.POST['choices'])
choice = Choice.objects.get(id = choice_id)
choice.total_votes += 1
choice.save()
request.session[question.id] = choice.id
return HttpResponseRedirect(question.get_results_url())
if request.method == 'GET':
try:
last_choice_id = request.session[question.id]
last_choice = Choice.objects.get(id = last_choice_id)
except KeyError, e:
last_choice = 0
choices = Choice.objects.filter(question = question)
payload = {'question':question, 'choices':choices,
'last_choice':last_choice}

return render('news/index.html', payload, request)

I want get both views in one template In news/index.html IWant get
poll in index page

Babatunde Akinyanmi

unread,
Sep 11, 2011, 1:57:18 PM9/11/11
to django...@googlegroups.com
Hi cha,
Programming in django is just programming in python. Views are just
normal python functions that are called from urls.py which contains
pattern which is also a function.
If you want both views in one file, do exactly that. You could cut and
paste the view in the second app and paste it in the first apps view
then adjust the pattern in your urls.py file.
Or better still you could create a new file and cut and paste the
functions in the first and second view into that new file and make the
adjustment in your urls.py file.
If you still don't understand the explanation you could copy and paste
the your urls file here or someone might be able to provide a link to
some resource on the web.
You need to be comfortable with programming with python for you to get
the hang of django.

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.
>
>

--
Sent from my mobile device

Petey

unread,
Sep 11, 2011, 2:02:51 PM9/11/11
to django...@googlegroups.com
You may merge the code in views.py and use 1 url for them.

You could use TemplateView() instead of render to response

cha

unread,
Sep 11, 2011, 5:17:47 PM9/11/11
to Django users
thank you very much
Babatunde Akinyanmi & petey
Im merge the code and its work

Mr Babatunde Akinyanmi
acutely I need be comfortable with programming with python
But I dont Know How to do that
can you help me and give me the best way to be comfortable and
professional programmer

Babatunde Akinyanmi

unread,
Sep 12, 2011, 1:41:39 AM9/12/11
to django...@googlegroups.com
This is way off topic for this group but......

To be comfortable with python, the online python documentation is a
very good resource. It seems like you're a programming noob so also
get books like pragmatic programmer and Python The Hard Way. Of course
there are many more good books on python programming but nobody learns
how to swim by reading books. In other words, the singular act that
will make you a better programmer is doing more programming. You can
also check github and the likes so you can read other people's code
and maybe improve them.

On 9/11/11, cha <moham...@gmail.com> wrote:

Reply all
Reply to author
Forward
0 new messages