Using query set in views.py

22 views
Skip to first unread message

Stanwin Siow

unread,
Feb 26, 2012, 4:57:26 AM2/26/12
to django...@googlegroups.com
Hello,

I was wondering if we are able to use querysets in views.py so i can use one of the columns in my method below:


def keyword_subscribe(request):
    keyword= ''
    if request.method == 'POST':
    subscription_days = "7"
new_keyword = request.POST.get('myTextField')
    new_keyword_subscribed = subscribe_keyword(username,subDay =7,KEYWORD_SET=frozenset())        
                                                
        response = simplejson.dumps({'new_keyword': new_keyword_subscribed})                                              
    else:
        html = form.errors.as_ul()
        response = simplejson.dumps({'success':'False', 'html':html})
    if request.is_ajax():
        return HttpResponse(response, mimetype='application/json')
    else:
        return HttpResponseRedirect("/")

Best Regards,

Stanwin Siow



Daniel Roseman

unread,
Feb 26, 2012, 5:25:04 AM2/26/12
to django...@googlegroups.com
Your question is not at all clear. You can use whatever you like in your view. What problem are you having?
--
DR.

Stanwin Siow

unread,
Feb 26, 2012, 5:33:32 AM2/26/12
to django...@googlegroups.com
Sorry if i have misled anyone.

Here's a better view on what i'm asking.

def get_keyword():
return Keyword.objects.all()

That method above returns me a list of Keyword items correct?

How then do i get a specific item from that list?

Keyword is a table in my database but i need a specific column's information in my views.py.

Hope that makes it clearer.

Best Regards,

Stanwin Siow

> --
> 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/-/1N2Cz2nv9wQJ.
> 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.
>

Stanwin Siow

unread,
Feb 26, 2012, 6:33:12 AM2/26/12
to django...@googlegroups.com
Ok disregard my previous question.

Here's the latest one.


I have the following form in my HTML:
<form method="POST" id="keywordForm" action="/keyword_subscribe/">
<label>Keyword: </label>
<input id="myTextField" type="text" size="50" value="Enter a new keyword for rosebud to crawl!">
</br>
<input type="submit" value="Submit" />
</form>

when the user presses the submit button, it will go to the method keyword_subscribe in views.py


The method is as shown:

def keyword_subscribe(request):
    if request.POST:
username = UserProfile.objects.all()
#userid = username.objects.all()
    subscription_days = "7"
new_keyword = request.POST.get('myTextField')
print new_keyword
    new_keyword_subscribed = subscribe_keyword(username,subscription_days,new_keyword)        
                                                
        response = simplejson.dumps({'new_keyword': new_keyword_subscribed})       
print new_keyword_subscribed                                      
    else:
       # html = form.errors.as_ul()
        response = simplejson.dumps({'success':'False'})
return HttpResponseRedirect("/accounts/login/")
    #if request.is_ajax():
     #   return HttpResponse(response, mimetype='application/json')
    #else:
     #   return HttpResponseRedirect("/")

Once in this method, i'm supposed to extract the word which the user entered in the textfield and store it in a variable called new_keyword

However, i've been getting NONE which means there's something wrong somewhere and i do hope someone can help me.

In addition, i would like to get the username which is stored in the UserProfile table in my database to be passed as a parameter to the next function too.

How then do i implement the queryset needed?

This should be clearer. 

Thank you.


Best Regards,

Stanwin Siow



On Feb 26, 2012, at 6:25 PM, Daniel Roseman wrote:

Daniel Roseman

unread,
Feb 26, 2012, 6:35:06 AM2/26/12
to django...@googlegroups.com

On Sunday, 26 February 2012 10:33:32 UTC, St@n wrote:
Sorry if i have misled anyone.

Here's a better view on what i'm asking.

def get_keyword():
   return Keyword.objects.all()

That method above returns me a list of Keyword items correct?

How then do i get a specific item from that list?

Keyword is a table in my database but i need a specific column's information in my views.py.

Hope that makes it clearer.

Best Regards,

Stanwin Siow


Presuming you mean a specific row rather than a specific column, you use one of the queryset filtering or slicing options.

eg to get the Keyword whose name field is 'foo':
    Keyword.objects.get(name='foo')

or to get all the Keywords whose name field starts with 'bar'
    Keyword.objects.filter(name__startswith='bar')

or to get the first Keyword in the database:
    Keyword.objects.all()[0]

etc.
As always, all this is explained in the tutorial and fully documented in the query method reference.
--
DR.

Ian Clelland

unread,
Feb 27, 2012, 2:38:22 AM2/27/12
to django...@googlegroups.com


On Sunday, February 26, 2012, Stanwin Siow wrote:
Ok disregard my previous question.

Here's the latest one.


I have the following form in my HTML:
<form method="POST" id="keywordForm" action="/keyword_subscribe/">
<label>Keyword: </label>
<input id="myTextField" type="text" size="50" value="Enter a new keyword for rosebud to crawl!">

Your immediate problem here is that your <input> element has an id, but no name attribute. That is why you are getting None when you try to retrieve it from the POST dictionary -- the browser never sent it to the server.

I'm certain that you have additional issues with your view function, but this is the reason for the error that you are seeing right now.

Ian
--
Regards,
Ian Clelland
<clel...@gmail.com>

doniyor

unread,
Feb 27, 2012, 3:36:13 AM2/27/12
to Django users
yes, as Ian said, you need name attr in your input so that you can
navigate to the real string input the user gives..



On 27 Feb., 08:38, Ian Clelland <clell...@gmail.com> wrote:
> On Sunday, February 26, 2012, Stanwin Siow wrote:
> > Ok disregard my previous question.
>
> > Here's the latest one.
>
> > I have the following form in my HTML:
> > * <form method="POST" id="keywordForm" action="/keyword_subscribe/">*
> > * <label>Keyword: </label>*
> > * <input id="myTextField" type="text" size="50" value="Enter a new
> > keyword for rosebud to crawl!">*
>
> Your immediate problem here is that your <input> element has an id, but no
> name attribute. That is why you are getting None when you try to retrieve
> it from the POST dictionary -- the browser never sent it to the server.
>
> I'm certain that you have additional issues with your view function, but
> this is the reason for the error that you are seeing right now.
>
> Ian
>
>
>
>
>
>
>
>
>
> > * </br>*
> > * <input type="submit" value="Submit" />*
> > * </form>*
>
> > when the user presses the submit button, it will go to the method
> > keyword_subscribe in views.py
>
> > The method is as shown:
>
> > *def keyword_subscribe(request):*
> > *    if request.POST:*
> > * username = UserProfile.objects.all()*
> > * #userid = username.objects.all()*
> > *    subscription_days = "7"*
> > * new_keyword = request.POST.get('myTextField')*
> > * print new_keyword*
> > *    new_keyword_subscribed =
> > subscribe_keyword(username,subscription_days,new_keyword)        *
> > *                                                *
> > *        response = simplejson.dumps({'new_keyword':
> > new_keyword_subscribed})       *
> > * print new_keyword_subscribed                                      *
> > *    else:*
> > *       # html = form.errors.as_ul()*
> > *        response = simplejson.dumps({'success':'False'})*
> > * return HttpResponseRedirect("/accounts/login/")*
> > *    #if request.is_ajax():*
> > *     #   return HttpResponse(response, mimetype='application/json')*
> > *    #else:*
> > *     #   return HttpResponseRedirect("/")*
>
> > Once in this method, i'm supposed to extract the word which the user
> > entered in the textfield and store it in a variable called new_keyword
>
> > However, i've been getting NONE which means there's something wrong
> > somewhere and i do hope someone can help me.
>
> > In addition, i would like to get the username which is stored in the
> > UserProfile table in my database to be passed as a parameter to the next
> > function too.
>
> > How then do i implement the queryset needed?
>
> > This should be clearer.
>
> > Thank you.
>
> > Best Regards,
>
> > Stanwin Siow
>
> > On Feb 26, 2012, at 6:25 PM, Daniel Roseman wrote:
>
> > Your question is not at all clear. You can use whatever you like in your
> > view. What problem are you having?
> > --
> > DR.
>
> > --
> > 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/-/1N2Cz2nv9wQJ.
> > To post to this group, send email to django...@googlegroups.com<javascript:_e({}, 'cvml', 'django...@googlegroups.com');>
> > .
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com <javascript:_e({}, 'cvml',
> > '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 post to this group, send email to django...@googlegroups.com<javascript:_e({}, 'cvml', 'django...@googlegroups.com');>
> > .
> > To unsubscribe from this group, send email to
> > django-users...@googlegroups.com <javascript:_e({}, 'cvml',
> > 'django-users%2Bunsu...@googlegroups.com');>.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.
>
> --
> Regards,
> Ian Clelland
> <clell...@gmail.com>

Stanwin Siow

unread,
Feb 27, 2012, 3:43:30 AM2/27/12
to django...@googlegroups.com
i see. Thanks guys.

I've got it working thanks to your inputs. 

Really appreciate it.

Best Regards,

Stanwin Siow



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