Re: How do you retrieve a username that is not in the URL?

2009-04-14 Thread Daniel Roseman

On Apr 14, 11:12 pm, stkpoi  wrote:
> I would like the url to not include the user's name.  For example, it
> would be "/submit" and not "/submit/joe_user".
>
> request.user.username is not working for me.
>
> # Model
> class Submission(models.Model):
>     title = models.CharField(max_length=200)
>     link = models.URLField()
>     user = models.ForeignKey(User)
>
> # Form
> class SubmissionForm(forms.Form):
>   title = forms.CharField(max_length=200)
>   link = forms.URLField()
>
> # View
> def submit_page(request):
>   if request.method == 'POST':
>     form = SubmissionForm(request.POST)
>     if form.is_valid():
>       submission = Submission.objects.get_or_create(
>       title=form['title'],
>       link=form['link'],
>       user=request.user.username
>       )
>       return HttpResponseRedirect('/submit/submit_success.html')
>   else:
>     form = SubmissionForm()
>
>   variables = RequestContext(request, {
>     'form': form, 'user':request.user.username
>   })
>   return render_to_response('submit/submission.html', variables)
>
> # Result: Error binding parameter 1 - probably unsupported type


You haven't shown any code that puts the username in the URL, or
explained where that error appears, so it's hard to diagnose your
problem. It might help if you showed your urls.py.

If you don't want to put the username in the URL, don't put it there.
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How do you retrieve a username that is not in the URL?

2009-04-14 Thread stkpoi

#urls.py
urlpatterns = patterns('',
(r'^$', main_page),
# ...
(r'^submit/$', submit_page),
(r'^submit/success/$', direct_to_template,
 { 'template': 'submit/submit_success.html' }),
)

The error occurs at the "user=request.user.username" line in views.py.

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



Re: How do you retrieve a username that is not in the URL?

2009-04-14 Thread James Bennett

On Tue, Apr 14, 2009 at 5:12 PM, stkpoi  wrote:
>    if form.is_valid():
>      submission = Submission.objects.get_or_create(
>      title=form['title'],
>      link=form['link'],
>      user=request.user.username
>      )

And right there's your problem. The value to put into a ForeignKey is
the actual User object itself, not its id or username or some other
attribute. The error message you're seeing is a consequence of putting
the wrong value in there.


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

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



Re: How do you retrieve a username that is not in the URL?

2009-04-15 Thread JL

Also, you might want to make sure you have an authenticated user (by
either decorating your view function with @login_required) or check
that the user in the request is either authenticated or not the
anonymous user (by including an if statement).

On Apr 14, 8:36 pm, James Bennett  wrote:
> On Tue, Apr 14, 2009 at 5:12 PM, stkpoi  wrote:
> >    if form.is_valid():
> >      submission = Submission.objects.get_or_create(
> >      title=form['title'],
> >      link=form['link'],
> >      user=request.user.username
> >      )
>
> And right there's your problem. The value to put into a ForeignKey is
> the actual User object itself, not its id or username or some other
> attribute. The error message you're seeing is a consequence of putting
> the wrong value in there.
>
> --
> "Bureaucrat Conrad, you are technically correct -- the best kind of correct."
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---