【问题标题】:How use TemplateView with 2 methods (get and post)如何通过 2 种方法(获取和发布)使用 TemplateView
【发布时间】:2019-10-25 18:33:09
【问题描述】:

我正在尝试在 Django 中使用 Templateview 来呈现一个页面,该页面具有添加到数据库和从数据库中检索一些信息并显示它的选项。我是根据https://www.youtube.com/watch?v=VxOsCKMStuw的教程编写的

views.py:

class TestView(TemplateView):
    template_name = 'app/sensor_name_tmpl.html'

    def get(self, request):
        form = SensorForm()
        posts = Sensor.objects.all()

        args = {'form': form, 'posts': posts}
        return render(request, self.template_name, args)


    def post(self, request):
        form = SensorForm(request.POST)
        if form.is_valid():
            form.save()
            text = form.cleaned_data['post']
            form = SensorForm()
            return redirect('sensor_name_tmpl:sensor_name_tmpl')

        args = {'form': form, 'text': text}
        return render(request, self.template_name, args)

urls.py:

urlpatterns = [
    path('', views.index, name='index'),
    url(r'^form1/$', views.get_sensor_name, name='GiveSensorName1'),
    #url(r'^form2/$', TestView.as_view(), name='sensor_name_tmpl.html'),
    path('form2/', TestView.as_view(), name='app/sensor_name_tmpl.html'),
    url(r'^nested_admin/', include('nested_admin.urls')),
]

HTML 模板:

    <!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        <script>
            $('#toggle').click(function() {
            $('form').toggle('slow');
            });
        </script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

    <title>Hello world!</title>
  </head>


  <body>
    <h3 class="text-success">Add Sensor</h3>
    <br>

<!--    <form style="display:none;" method="post">-->
        <form method="post">
        {% csrf_token %}
        <div class="row align-items-center">
            <div class="col-sm-8">
                <table>
                   {{ form1.as_table}}
                </table>
                <div class="mx-sm-2">
                    <input type="submit" value="Submit">
                </div>

                <br>
                <br>

                <h3 class = "text-success">Add Sensor View</h3>
                <table>
                   {{ form2.as_table}}
                </table>
                  <div class="mx-sm-2">
                        <input type="submit" value="Submit">
                  </div>
                <br>
                <br>

                <h3 class="text-success">View Sensors</h3>

                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">Sensor ID</th>
                            <th scope="col">Sensor Name</th>
                        </tr>
                    </thead>
                <tbody>
                {%for obj in obj%}
                    <tr>
                        <td>{{obj.sensor_id}}</td>
                        <td>{{obj.sensor_name}}</td>
<!--                        <th scope="row">1</th>-->
                    </tr>
                {% endfor %}
                </tbody>
                </table>





            </div>
        <div>
    </form>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>

  </body>
</html>

页面呈现模板,但不使用表单域或数据库中的数据填充它。

【问题讨论】:

  • 您如何访问 HTML 中的上下文?编辑您的帖子并添加您的 HTML 代码
  • 使用 FormView 。将TemplateView 替换为FormView
  • @NalinDobhal,谢谢,意识到 HTML 模板出了什么问题,现在可以使用了

标签: django forms django-views


【解决方案1】:

问题在于 HTML 模板,其中 form1 和 form2 现在已替换为 form,for 循环中的“obj”已替换为“posts”。模板现在如下所示:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <script src="https://code.jquery.com/jquery-1.10.2.js"></script>
        <script>
            $('#toggle').click(function() {
            $('form').toggle('slow');
            });
        </script>
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">

    <title>Hello world!</title>
  </head>


  <body>
    <h3 class="text-success">Add Sensor</h3>
    <br>

<!--    <form style="display:none;" method="post">-->
        <form method="post">
        {% csrf_token %}
        <div class="row align-items-center">
            <div class="col-sm-8">
                <table>
                   {{ form.as_table}}
                </table>
                <div class="mx-sm-2">
                    <input type="submit" value="Submit">
                </div>

                <br>
                <br>

                <h3 class = "text-success">Add Sensor View</h3>
                <table>
                   {{ form.as_table}}
                </table>
                  <div class="mx-sm-2">
                        <input type="submit" value="Submit">
                  </div>
                <br>
                <br>

                <h3 class="text-success">View Sensors</h3>

                <table class="table">
                    <thead>
                        <tr>
                            <th scope="col">Sensor ID</th>
                            <th scope="col">Sensor Name</th>
                        </tr>
                    </thead>
                <tbody>
                {%for obj in posts%}
                    <tr>
                        <td>{{obj.sensor_id}}</td>
                        <td>{{obj.sensor_name}}</td>
<!--                        <th scope="row">1</th>-->
                    </tr>
                {% endfor %}
                </tbody>
                </table>





            </div>
        <div>
    </form>


    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->

    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>

  </body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-05
    • 1970-01-01
    • 2021-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-11
    • 1970-01-01
    相关资源
    最近更新 更多