单击按钮时,在Django模板中更改表单的位置

时间:2015-01-27 11:24:07

标签: python html django

我会尽力解释我的问题。我有一个Django模板,如下所示:

<!DOCTYPE html>
<html>
    <head>
    <title>Developer homepage</title>
    </head>
    <body>
        <h3>Developer homepage</h3>
        <p>Welcome, {{user.get_username}}</p></br>
    <fieldset>
        <legend><strong> Games in your inventory</strong> </legend>
        <table>
            <col width="100">
            <col width="100">
            <col width="100">
            <col width="100">
            <col width="400">
            <tr>
                <th>Game ID</th>
                <th>Name</th>
                <th>Category</th>
                <th>Username</th>
                <th>URL</th>
            </tr>
            {% for game in list_of_games %}
            <tr>
                <td align="center">{{game.id}}</td>
                <td align="center">{{game.name}}</td>
                <td align="center">{{game.category}}</td>
                <td align="center">{{game.developer.username}}</td>
                <td align="center">{{game.url}}</td>
            </tr>
        {% endfor %}
        </table>
    </fieldset>
    {% if saved %}  
    <script>alert("Game successfully added!")</script>
    {% endif %}
    <form action="/addgame/" method="post">{% csrf_token %}
        {{ form.as_p }}
        <input type="submit" name="submit" value="Add game"/>           
    </form>
    <br>
        <form action="/logout/" method="post">{% csrf_token %}
            <input type="submit" value="Logout" />
        </form>
    </body>
</html>

您必须从上面的页面中注意到有一个显示列表的HTML图例。在列表部分下,有一个名为&#34;添加游戏&#34;的按钮。单击此按钮时,需要在列表后面显示一个表单。目前,表单正在删除列表,并显示在页面顶部。如何使表单显示在列表之后(下方)? 我希望我已经说清楚了,如果没有,请告诉我。我会很高兴得到任何帮助!

views.py(相关部分):

def addgame(request):

    c={}
    c.update(csrf(request))
    saved = False
    #developer = request.POST['developer']
    if request.method == 'POST':
        form = GameForm(data=request.POST)
        if form.is_valid():
            game = form.save(commit=False)
            game.developer = request.user
            print(game.developer)
            game.save() 
            saved = True
        else:
            print(form.errors)
    else:
        form = GameForm()



def login_view(request):

    ...
    elif usertype == 'developer' and user.usertypes.usertype == usertype:
            list_of_games = Games.objects.filter(developer=user)
            return render_to_response('gamestore/developer_homepage.html',context_instance=RequestContext(request, {'list_of_games':list_of_games}))
    ...

0 个答案:

没有答案