-4

I want to get the current profile viewing username and save it in instance.user. The problem is that I want to get the username of the user that is going to be rated for other users.

Here is my code:

def new_rate(request):
    if request.method == 'POST':
        form = RateForm(request.POST)
        if form.is_valid():
            instance = form.save(commit=False)
            try:
                instance.rater = request.user
    --------->  instance.user = !!!!! GET THE CURRENT  PROFILE VIEWING USERNAME !!!!!
                instance.validate_unique()
                instance.save()
                form.save_m2m()
                return HttpResponseRedirect('/user/' + request.user.username)

           
TylerH
  • 20,799
  • 66
  • 75
  • 101
kostas1987
  • 57
  • 1
  • 6
  • Why do you want to save it to the user that is logged in and you should also make this request a GET instead of a POST. – Henrik Andersson Jan 05 '15 at 16:10
  • Firstly, DON'T SHOUT, it's rude, and don't use exclamation marks. And secondly, you haven't provided any useful information. What is the "current profile viewing username"? Where is it coming from? – Daniel Roseman Jan 05 '15 at 16:12
  • You should ideally be sending the userid, or user slug as a part of the URL. Example `/rating/new//` and handling it that way. – karthikr Jan 05 '15 at 16:12
  • 1
    @limelights that is completely wrong. Why should a form submission be a GET? – Daniel Roseman Jan 05 '15 at 16:14
  • @DanielRoseman It shouldn't but this form submission should happen when the user is to be rated. What OP wants is to get the actual user id first and then post the rating. My wording could have been better. – Henrik Andersson Jan 05 '15 at 16:16
  • sorry about my shouting. it's about emphasizing where is my problem – kostas1987 Jan 05 '15 at 18:48

1 Answers1

0

You can only get request.user.username if your user is authenticated. To check it use request.user.is_authenticated().

So I think it would look somewhat like this:

def new_rate(request):
    if request.method == 'POST':
        form = RateForm(request.POST)
        current_user = request.user
        if form.is_valid() and current_user.is_authenticated():
            instance = form.save(commit=False)
            try:
               instance.rater = current_user
               instance.user = current_user
               instance.validate_unique()
               instance.save()
               form.save_m2m()
               return HttpResponseRedirect('/user/' + current_user.username)

But to me, your design sounds flawed. If you already use request.POST, you should POST the current profile's ID.

If for example you're viewing URL /users/500 and you have a form. Send the ID 500 and then in the view it should get the current profile user like:

from django.contrib.auth import get_user_model

User = get_user_model()

def new_rate(request):
    if request.method == 'POST':
        form = RateForm(request.POST)
        current_user = request.user
        profile_id = request.POST['profile_id']

        if form.is_valid() and current_user.is_authenticated():
            instance = form.save(commit=False)
            profile_user = User.objects.get(pk=profile_id)

            try:
               instance.rater = current_user
               instance.user = profile_user
               instance.validate_unique()
               instance.save()
               form.save_m2m()
               # it should  return us back to that profile, not the request.user's
               return HttpResponseRedirect('/user/' + profile_user.username)
Anton Antonov
  • 1,217
  • 2
  • 14
  • 21