Django发布数据以使用不同的变量重定向到同一页面

时间:2019-02-15 04:58:09

标签: python django python-3.x django-forms

我的主页使用其player_name变量将发布数据发送到其他玩家的页面。但是,当我尝试通过播放器的页面发送其他播放器的帖子数据来执行相同的操作时,它不会重定向并传递更新的变量。为什么会这样,我该如何解决?

我的url.py     从django.conf.urls导入URL     从网站导入视图中

urlpatterns = [
    url(r'^$', views.HomePage.as_view()),
    url(r'^player/(\w+)', views.PlayerPage.viewProfile, name = 'player_name'),

]

我的view.py

class HomePage(ListView):
    template_name = 'index.html'

    def post(self, request):
        form = searchUser(request.POST or None)

        context = { 'form': form }

        if form.is_valid():
            player = form.cleaned_data['player']
            context.update({'player': player})

        return HttpResponseRedirect("player/" + player)

    def get_queryset(self):
        return 

class PlayerPage(ListView):
    template_name = 'player.html'

    def viewProfile(request, player_name):
        return render(request, 'player.html', {'player_name' : player_name})


    def post(self, request):
        form = searchUser(request.POST or None)

        context = { 'form': form }

        if form.is_valid():
            player = form.cleaned_data['player']
            context.update({'player': player})

        return HttpResponseRedirect("player/" + player)

我的player.html

<html>
    <body>
            {% block body %}
            <form role="form" method="post">
                {% csrf_token %}
                  <input name="player" placeholder="Search Player" type="search">
                </div>
              </div>
            </form>
            {% endblock %}
            <br>
            {{player_name}}
    </body>
</html>

0 个答案:

没有答案