【问题标题】:Django / HTTP - Sending query string with a POST request - Is it not recommended?Django / HTTP - 使用 POST 请求发送查询字符串 - 不推荐吗?
【发布时间】:2014-06-21 02:08:52
【问题描述】:

这是我的models.py:

class Blog(models.Model):
    user = models.ForeignKey(User)
    votes = models.IntegerField(default=0)

class BlogExtended(models.Model):
    blog = models.ForeignKey(Blog)
    usersVoted = models.ManyToManyField(User)

基本上,每个博客都有一个创建该博客的用户。如果用户喜欢某人的博客,他们可以投票给它。每次博客获得投票时,“投票”计数器都会增加一个。在我的模板中,我不希望投票按钮成为仅显示“投票”的按钮。我希望投票按钮是一个图像。由于一旦用户单击投票按钮,我将更改数据库(将“投票”字段增加一个)我需要使用 POST 请求。 所以这就是我最初的模板(我最初使用 GET 请求):

{% if Blogs %} <!-- Blogs is a list of blogs -->
     <ul class="blogs">
     {% for blog in Blogs %}
         <li>
             <a href="/vote/?id={{ blog.id }}" class="vote"><img src="images/voteButton.jpg" </a>

这是我处理 /vote/ url 的视图:

def voteView(request):
     if request.GET.has_key('id'): #if it is a GET request which has the blogs id
         try:
             id = request.GET['id']
             blog = Blog.objects.get(id=id) #if the Blog is found
             blog.votes += 1 #make a change to the 'votes' field in the DB
             blog.blogextended_set.usersVoted.add(request.user) #make a change in the 'usersVoted' field in the DB
             blog.save()

但正如您所见,我使用的是 GET 而不是 POST。我意识到我需要使用 POST,因为我正在更改数据库,所以这是我尝试使其成为 POST 请求:

<form method="post" action="/vote/">{% csrf_token %}
    <button type="submit">
        <img src="images/voteButton.jpg" alt="vote" /> 
            <!-- I used button rather than input because with button, I was able to replace the button with an image.. I was unable to replace the button with an image -->
    </button>
</form>

这种形式基本上不向 /vote/ 发送任何信息(它只发送当前用户对象:request.user)。我还需要发送博客的 ID。通过在 GET 请求的 URL 中添加查询字符串,我能够以初始方式发送 ID。我能想到的在我的 POST 请求中添加博客 ID 的唯一方法是在 URL 中添加一个查询字符串,所以我的表单会像这样操作:

<form method="post" action="/vote/?id={{ blog.id }}">

我意识到使用 POST 请求发送查询字符串是我以前从未见过/做过的事情。如果我发送带有 POST 请求的查询字符串可以吗?还是不推荐?使用 POST 请求发送查询字符串会出现什么问题?

其他帮助:如果不推荐,任何人都可以提出另一种与 POST 请求一起发送附加信息的方法吗?

【问题讨论】:

    标签: django http post request query-string


    【解决方案1】:

    您只需在表单中添加另一个字段:

    <input type="hidden" name="blog_id" value="{{ blog.id }}" />
    

    整个表格:

    <form method="post" action="/vote/">{% csrf_token %}
        <button type="submit">
            <img src="images/voteButton.jpg" alt="vote" /> 
        </button>
        <input type="hidden" name="blog_id" value="{{ blog.id }}" />
    </form>
    

    那么,您的视图应如下所示:

    def voteView(request):
         if request.POST.has_key('blog_id'): 
             try:
                 id = request.POST['blog_id'] 
                 ...
    

    【讨论】:

      【解决方案2】:

      更好的解决方案是将博客 ID 放在表单的隐藏字段中,如下所示:

      <form method="post" action="/vote/">
          {% csrf_token %}
          <input type="hidden" name="id" value="{{ blog.id }}">
          <button type="submit">
              <img src="images/voteButton.jpg" alt="vote" />
          </button>
      </form>
      

      在您看来,您将能够提取使用request.POST['id'] 投票支持的博客帖子的 ID。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-05-14
        • 2020-08-16
        • 2022-06-19
        • 1970-01-01
        • 2013-06-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多