form.Forms + POST + forms.ChoiceField

185 views
Skip to first unread message

Barry Morrison

unread,
Apr 1, 2012, 10:21:38 PM4/1/12
to django...@googlegroups.com
Full Disclosure: I've been using Python for 5-6 months. Django for maybe 3

I have this: https://gist.github.com/a934fe08643e0c3ee017

In an effort to teach myself, I'm simply building a Django app. For teaching purposes only. It's an aggregator of favorites across multiple sites. Right now, I'm simply dealing with saved items from Reddit. 

The problem I'm having is the drop-down selector POST'ing and creating a the new URL. 

http://imgur.com/a/FL32I << Screen shots

1) root or /feeds/
2) Select Reddit from drop-down
3) Displays Reddit items only /feeds/Reddit/
4) Select 'Python' from drop-down, displays items from r/Python with URL /feeds/Reddit/Python

This works (manually entering URL's), the drop down does not. 

I currently get "Error 1" from the gist. 

On views.py, if I uncomment lines 16 - 19, and comment the lines 21 - 23, I get a different error: "Error2" from the gist.  What I don't understand regarding the 2nd error, is that it is in fact returning: source: u'Reddit' and that exists in the Model, so it makes me think it's expecting something other than u'Reddit'.  

I'm at a loss, so this is me asking for help.  Sorry for the long post and crazy amounts of information. 

Any help is GREATLY APPRECIATED! 

Thanks! 

POST

Sergiy Khohlov

unread,
Apr 2, 2012, 5:18:29 AM4/2/12
to django...@googlegroups.com
So much code ;-)
at first I'm proposing to add some code for form.is_valid block
for example :
if form.is_valid():
source = Sources.objects.get(name=source)
allPosts = Posts.objects.filter(PostSource=source)
subreddits = SubReddits.objects.all()
source = form.cleaned_data['foo']
return render_to_response('feeds.html',
{'allPosts': allPosts,
'subreddits': subreddits},
context_instance=RequestContext(request))
else:
print form.error

This will help to catch error with form .

Also it is not correct to use this construction:

class SourceSelection(forms.Form):
sources = Sources.objects.all()
foo = forms.ChoiceField(choices=sources)

This mean that you never add any sources. or sources object are add
before starting application. For dynamic loading objects in the form
use form constructor.
and last one point :

Please separate views and form in the different files : views.py forms.py

2012/4/2 Barry Morrison <bdmor...@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/django-users/-/zXNmsoqAfdMJ.
> 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.

Barry Morrison

unread,
Apr 2, 2012, 9:33:57 AM4/2/12
to django...@googlegroups.com
Too much code? I'd rather too much than not enough, but if it'd help, I can cut down on future posts.

Thanks for the information/guidance.  I'll try your suggestions and post my findings. 

Much appreciated!

> django-users+unsubscribe@googlegroups.com.

Sergiy Khohlov

unread,
Apr 2, 2012, 4:34:58 PM4/2/12
to django...@googlegroups.com
Ok.
I'm not definitely sure what you want to have but I'm proposing
to do simple project with next functionality:

1) prepare list of the links
2) ability to click at the link and receive some additional
information about link
3) button for deleting of the link
4) button for creating new one

Which are be covered :
class based view
generic view
passing values from view to form or template

If you are interested we can start. Also if you would like to add
some other areas let me know

Thanks,
Serge

2012/4/2 Barry Morrison <bdmor...@gmail.com>:

>> > django-users...@googlegroups.com.


>> > For more options, visit this group at
>> > http://groups.google.com/group/django-users?hl=en.
>

> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/django-users/-/S3mhDHD8nu4J.


>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users...@googlegroups.com.

Barry Morrison

unread,
Apr 2, 2012, 7:24:07 PM4/2/12
to django...@googlegroups.com
If you could point me to a more recent tutorial, I'm more than willing to go through it. 

Basically what I have now, is a list of links. I want to sort those links based on source (Reddit) then subreddit (Python).  The source and subreddits are fed from my DB and I want to select them from a drop-down menu.  


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

Sergiy Khohlov

unread,
Apr 3, 2012, 3:35:31 AM4/3/12
to django...@googlegroups.com
try to use django generic class view

https://docs.djangoproject.com/en/dev/topics/class-based-views/

Sorting is added by adding next construction to the your class view:


class yourclassview(ListView):
"""
model = yourmodel

context_object_name = 'object_or_list'

template_name = 'yourtemplate.html'

def get_queryset(self):
qs = yourmodel.objects.all().order_by('sortingcriteria
return qs


2012/4/3 Barry Morrison <bdmor...@gmail.com>:

>> >> > django-users...@googlegroups.com.


>> >> > For more options, visit this group at
>> >> > http://groups.google.com/group/django-users?hl=en.
>> >
>> > --
>> > You received this message because you are subscribed to the Google
>> > Groups
>> > "Django users" group.
>> > To view this discussion on the web visit
>> > https://groups.google.com/d/msg/django-users/-/S3mhDHD8nu4J.
>> >
>> > 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.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To view this discussion on the web visit

> https://groups.google.com/d/msg/django-users/-/Tgz68neJN0AJ.


>
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to

> django-users...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages