I'm facing a problem with redirecting in class-based success_url

I'm working with Django 2.2 and I'm facing a problem with redirecting in class-based success URL.

enter code here - view.py

class LetterFormView(CreateView):
   template_name = 'post.html'
   model = Letter
   form_class = LetterForm


def get_success_url(self):
    return reverse ('mainapp:user-profile')

and the url.py

enter code here- urls.py

path('profile/<username>/', views.UserProfilePage.as_view(), name='user-profile'),
class Letter(models.Model):
    author = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE, default=1)
    subject = models.CharField(max_length=100, null=False, blank=False)
    description = models.CharField(max_length=300, null=True, blank=True)
    date_time = models.DateTimeField(auto_now_add=True)
    Text = models.TextField()

This is how my model looks like

Now I'm not sure how to pass the username argument in here.

You can pass url parameters in reverse function using kwargs.

reverse ('mainapp:user-profile', kwargs={'username':self.object.username})

Lauv - I Like Me Better [Official Video], Lauv's debut album, ~how i'm feeling~ is available now: http://lauv.lnk.to/ howimfeelingYD I Duration: 3:26 Posted: Aug 10, 2017 Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family.

You seem to be missing a level of indentation on the get_success_url method; it needs to fall within class LetterFormView:

class LetterFormView(CreateView):
    template_name = 'post.html'
    model = Letter
    form_class = LetterForm


    def get_success_url(self):
        return reverse ('mainapp:user-profile')

Also, be sure to indent with four spaces to adhere to PEP-8; you seem to be indenting with three spaces within the class.

I (Kendrick Lamar song), noun, plural I's or Is, i's or is. the ninth letter of the English alphabet, a vowel. any spoken sound represented by the letter I or i, as in big, nice, or ski. something� I 1 (ī) pron. Used to refer to oneself as speaker or writer. n. pl. I's The self; the ego. [Middle English, from Old English ic; see eg in Indo-European roots.] Usage Note

you can try on view code

class LetterFormView(CreateView):
    template_name = 'post.html'
    model = Letter
    form_class = LetterForm

    def get_success_url(self):
       return reverse('mainapp:user-profile', kwargs={'username': self.object.username')})

self.object.username please use how to condition with model structure on your project

and than on urls code don't forget to initilize data type on username argument

path('profile/<str:username>/', views.UserProfilePage.as_view(), name='user-profile'),

I, Create an account or log in to Instagram - A simple, fun & creative way to capture, edit & share photos, videos & messages with friends & family. The sixteenth letter of the Hungarian alphabet, called í and written in the Latin script.

Instagram, The ninth letter of the basic modern Latin alphabet. I (lower case ı). The letter i without a dot above, in both the upper case and the lower case versions. î (lower case, upper case Î) The twelfth letter of the Romanian alphabet, called î or î din i and written in the Latin script.

I, I definition is - the 9th letter of the English alphabet. How to use i in a sentence. me or I? I have a PHP source file where  characters automatically got added in! I don't know from where they have come. I'm not getting any parse errors but it results in weird behavior in the execution

I, Windows is either a registered trademark or a trademark of Microsoft Corporation in the United States and/or other countries. Mac is a trademark of Apple Inc. Bir hesap oluştur veya Instagram'a giriş yap - Fotoğraf ve videolar çekip düzenlemenin, bu fotoğrafları, videoları ve mesajları arkadaşlarınla ve akrabalarınla paylaşmanın basit, eğlenceli ve yaratıcı yolu.

Comments
  • Hello, thank you for your support. but it gives an error 'Letter' object has no attribute 'username'. Do you know how to fix it? I have added the model in the question too, in case if you need to see it.
  • Hello, thank you for your support. but it gives an error 'Letter' object has no attribute 'username'. Do you know how to fix it? I have added the model in the question too, in case if you need to see it.
  • try this self.object.author or self.object.author.username