0

これは私がする必要があることです。販売というボタンがあります。ユーザーがログインしている場合は、通常の手順を実行し、ユーザーが販売できるようにします。ユーザーがログインしていない場合は、ログインページにリダイレクトする必要があります。ユーザーがログインした後、ログインしたユーザーが実行する通常の手順をリダイレクトします。これは、ユーザーのユーザー名を含むurlで、この「username/sell」のように販売します。

これは、認証されたユーザーとして正常に機能したビューでした

@login_required()
def UserSell(request,username):

    thegigform=GigForm()
    theuser=User.objects.get(username=username)
    if request.method=='POST':
         gigform=GigForm(request.POST,request.FILES)
          if gigform.is_valid():
        gigform.title=gigform.cleaned_data['title']
        gigform.description=gigform.cleaned_data['description']
        gigform.more_info=gigform.cleaned_data['more_info']
        gigform.time_for_completion=gigform.cleaned_data['time_for_completion']
        #need to change this, shouldnt allow any size image to be uploaded
        gigform.gig_image=gigform.cleaned_data['gig_image']
        #commit=False doesnt save to database so that I can add the current user to the gig
        finalgigform=gigform.save(commit=False)
        finalgigform.from_user=theuser
        finalgigform.save()
        return HttpResponseRedirect('done')

else:
    gigform=GigForm()
context=RequestContext(request)
return render_to_response('sell.html',{'theuser':theuser,'thegigform':gigform},context_instance=context)

ここにURLがあります

url(r'^(?P<username>\w+)/sell$','gigs.views.UserSell', name='sell'),

次にテンプレート

<a href="{% url sell user.username %}"><button type="button">Start Selling!</button></a>

ログインしたユーザーだったので、これはうまく機能しました。その後、匿名ユーザーとして別のブラウザーで試してみると、匿名ユーザーにはユーザー名がないことがすぐにわかりました。そのため、ビュー、URL、およびテンプレートを変更して、ユーザー。その後、デコレータがログインページにリダイレクトした後、ログインを試みるまでは正常に機能しました。ログイン後の{{next}}URLは、「user/sell」という絶対パスです。それに関する問題は、ユーザー名の代わりにユーザーを使用する更新されたビューを使用すると、「AnonymousUser/sell」にリダイレクトされることです。それは私の見解の問題だと思いますが、誰かが助けてくれますか。最近ログインしたユーザーのように、ログイン後のリダイレクトを「user/sell」にする必要があります。

4

1 に答える 1

0

ユーザーがログインすると、システムがユーザー名にアクセスできるようになるため、ユーザー名を要求する必要はないと思います。

@login_required()
def UserSell(request):
    ..........

    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('app_name:login'))
    else:
        context=RequestContext(request)
        return render_to_response('sell.html',{'theuser':request.user,'thegigform':gigform},context_instance=context)

ユーザーを表示したい場合は、「request.user」または「request.user.username」を入力してください

于 2013-02-05T06:39:02.457 に答える