0
votes

I have started learning Django, I'm not sure how to handle HttpResponseRedirect().

Here is the code snippet of the profile.html.

<form method="post" action="post/">
    {% csrf_token %}
    <div class="col-md-8 col-md-offset-2 fieldWrapper">
        {{ form.text.errors }}
        {{ form.text }}
    </div>
    {{ form.country.as_hidden }}
    <div>
        <input type="submit" value="post">
    </div>
</form>

As you can see on the snippet code, when I click submit button, it goes to http://localhost:8000/user/sapphire/post/ I guess.

Here is the snippet code in urls.py.

from tweets.views import PostTweet

urlpatterns = [
    url(r'^user/(\w+)/$', Profile.as_view()),
    url(r'^user/(\w+)/post/$', PostTweet.as_view()),
]+ static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

As you can see, when I click submit button, we navigate to the PostTweet class in views.py.

Here is the snippet code in views.py.

from django.shortcuts import render
from django.views.generic import View
from .models import Tweet
from user_profile.models import User
from .forms import TweetForm

class PostTweet(View):
    def post(self, request, username):
        form = TweetForm(self.request.POST)
        if form.is_valid():
            user = User.objects.get(username = username)
            tweet = Tweet(text = form.cleaned_data['text'], user = user, country = form.cleaned_data['country'])
            tweet.save()
            words = form.cleaned_data['text'].spilt(" ")
            for word in words:
                if word[0] == "#":
                    hashtag, created = HashTag.objects.get_or_create(name = word[1:])
                    hastag.tweet.add(tweet)
        return HttpResponseRedirect('/user/' + username)

As you can see, when I click submit button, I guess it goes to http://localhost:8000/user/sapphire/, and render another tweet I have added. But it doesn't work, exception occurs.

ValueError at /user/sapphire/post/, Exception Value: The view tweets.views.PostTweet didn't return an HttpResponse object. It returned None instead.

How can I handle it. To be honest, I'm not sure what HashTag means. In another tutorial, in order to use hashtag in django, I have to install and setup hashtag module, but I didn't. Please tell me how I handle this error and get idea of hashtag.

Thanks for your time.

1
There is definetly something wrong here. You are using HTTPResponseRedirect but you have not imported it. I guess python is not reaching upto that line or it would show "not defined" error. - PyManiac
Neither your error or code contain anything at all to do with HashTag. Why do you mention it? - Daniel Roseman
Thanks for your help. I've imported from django.http import HttpResponseRedirect on the top of views.py file, but same exception occurs. Please tell me why. - Yuiry Kozlenko
Hi, Daniel, I mean ** hashtag, created = HashTag.objects.get_or_create(name = word[1:])**. I'm not sure hashtag & HashTag.objects. - Yuiry Kozlenko

1 Answers

0
votes

It looks like you aren't using a Named Group in your URL.

If you add username to the URL: ^user/(?P<username>[\w+])/$' you should be able to use that value in the view.