Django表单字段将不显示

Django表单字段将不显示,django,forms,modelform,django-context,Django,Forms,Modelform,Django Context,我正在学习Django关于代码学校的基础教程,我已经很好地理解了代码学校的内容,并且在出现问题时一直在思考,直到现在。本教程构建了一个tic-tac-toe游戏 我正在尝试添加在用户轮到时进行移动的功能 我的相关代码如下。我已经把它缩小到我认为它与使用上下文有关的程度,因为如果我取出它并尝试下面的内容,它将显示表单。我想确保我的表单是正确的,如果这样做的话,它似乎显示正确 if request.method == 'POST': form = MoveForm(data=request.

我正在学习Django关于代码学校的基础教程,我已经很好地理解了代码学校的内容,并且在出现问题时一直在思考,直到现在。本教程构建了一个tic-tac-toe游戏

我正在尝试添加在用户轮到时进行移动的功能

我的相关代码如下。我已经把它缩小到我认为它与使用上下文有关的程度,因为如果我取出它并尝试下面的内容,它将显示表单。我想确保我的表单是正确的,如果这样做的话,它似乎显示正确

if request.method == 'POST':
    form = MoveForm(data=request.POST, instance=Move(game=game))
    if form.is_valid():
        form.save()
        return redirect('tictactoe_game_detail', pk=pk)
else:
    form = MoveForm()
return render(request, "tictactoe/game_do_move.html", {'form': form})
看到了吗?它显示表单,但仅显示表单:

我希望它显示在提交按钮上方的框中,但由于某些原因,它不在那里

我很有信心表格是可以的。我也试着这样做,只是想看看这些字段是否不存在,或者上下文中是否有其他内容,但它将x,y显示为字段

我已经盯着这个看了一天了,我不知道我做错了什么。我正在使用Django 1.8.1。(本教程是针对1.6的,但到目前为止似乎还可以。迁移而不是syncdb似乎是我注意到的最大变化之一)

谢谢你的帮助

守则:

models.py views.py game_do_move.html url.py
urlpatterns=patterns('tictactoe.views',
url(r“^invite$”,“新邀请”,name='tictactoe'U invite'),
url(r“^invitation/(?P\d+)/$”,“accept_invitation”,name='tictactoe_accept_invitation'),
url(r“^game/(?P\d+)/$”,“game\u detail”,name='tictactoe\u game\u detail'),
url(r'^game/(?P\d+)/do_move$,'game_do_move',name='tictactoe_game_do_move'),
)

问题出在views.py中。行中:

返回渲染(请求“tictactoe/game\u do\u move.html”,{'game':game})

{'game':game}
部分是
上下文。对于任何
GET
请求(如呈现空白表单),您不会呈现
form
上下文变量

将该行更改为:

返回渲染(请求“tictactoe/game\u do\u move.html”,上下文)

它应该会起作用。将空白<代码>窗体< /代码>对象添加到行

中的上下文变量
else:
    context['form'] = MoveForm()
但是如果不将其传递给
render
方法,它将不会出现在模板中

@login_required
def game_detail(request, pk):
    game = get_object_or_404(Game, pk=pk)
    if game.is_users_move(request.user):
        return redirect('tictactoe_game_do_move', pk=pk)
    return render(request, "tictactoe/game_detail.html", {'game': game})


@login_required
def game_do_move(request, pk):
    game = get_object_or_404(Game, pk=pk)
    if not game.is_users_move(request.user):
        raise PermissionDenied
    context = {'game': game}
    if request.method == 'POST':
        form = MoveForm(data=request.POST, instance=Move(game=game))
        context['form'] = form
        if form.is_valid():
            form.save()
            return redirect('tictactoe_game_detail', pk=pk)
    else:
        context['form'] = MoveForm()
    return render(request, "tictactoe/game_do_move.html", {'game': game})
{% extends "tictactoe/game_detail.html" %}
{% load staticfiles %}
{% load crispy_forms_tags %}

{% block styling %}
    {{ block.super }}
    <style type="text/css">
        .tictactoe-cell.empty:hover {
            background-color: #48CA3B;
            cursor: pointer;
        }
    </style>
{% endblock styling %}

{% block moveform %}
        {{ block.super }}
    <div class="well col-md-6">
        <form action="" method="post">
            {% csrf_token %}
            {{ form | crispy }}
            <button type="submit">Submit</button>
        </form>
    </div>
{% endblock moveform %}
{% extends "base.html" %}
{% load staticfiles %}

{% block title %}
    Tic-Tac-Toe game: {{ game.first_player }} vs {{ game.second_player }}
{% endblock title %}

{% block styling %}
    {{ block.super }}
    <link rel="stylesheet"
          href="{% static 'bootstrap/font-awesome-4.3.0/font-awesome-4.3.0/css/font-awesome.min.css' %}">
    <style type="text/css">
        .tictactoe-cell {
            background-color: #debb27;
        }

        #last-move {
            background-color: #DF6E1E;
        }
    </style>
{% endblock styling %}

{% block content %}
    <h3>Game: {{ game }}</h3>
    <div class="col-sm-9">
        <table class="table table-bordered" style="width: 60px; border-width: 2px">
            {% for line in game.as_board %}
                <tr>
                    {% for square in line %}
                        <td class="tictactoe-cell {% if not square %}empty{% endif %}"
                            style="width: 20px; height: 20px">
                            {{ square }}
                        </td>
                    {% endfor %}
                </tr>
            {% endfor %}
        </table>
        {% block moveform %}{% endblock moveform %}
    </div>
{% endblock content %}
class MoveForm(ModelForm):
    class Meta:
        model = Move
        exclude = ['game', 'by_first_player', 'comment']
urlpatterns = patterns('tictactoe.views',
                       url(r'^invite$', 'new_invitation', name='tictactoe_invite'),
                       url(r'^invitation/(?P<pk>\d+)/$', 'accept_invitation', name='tictactoe_accept_invitation'),
                       url(r'^game/(?P<pk>\d+)/$', 'game_detail', name='tictactoe_game_detail'),
                       url(r'^game/(?P<pk>\d+)/do_move$', 'game_do_move', name='tictactoe_game_do_move'),
                       )
else:
    context['form'] = MoveForm()