【问题标题】:django view 'GET' does not work, 'POST' does workdjango view 'GET' 不起作用,'POST' 起作用
【发布时间】:2018-10-30 02:29:38
【问题描述】:

我不知道为什么,但是如果我尝试“获取”此视图,我会收到此错误:

django.urls.exceptions.NoReverseMatch: Reverse for 'profile-update-name' with arguments '('',)' not found. 1 pattern(s) tried: ['ce_profiles/profile\\-update\\-name/(?P<pk>[0-9]+)/$']

但是如果提交一个 'POST' 请求,一切都很好。 ???

这是工作流程:

首先我访问这个视图和模板:

def customer_profile(request, pk):
    name = get_object_or_404(CEName, id=pk)
    return render(
        request,
        'customer/customer_profile.html',
        {'profile_name': name}
    )

{% block content %}
  {% include 'ce_profiles/name_header.html' %}
  <div id="name" class="container d-flex justify-content-between pt-1">
    {% include 'ce_profiles/profile_name.html' %} <!-- displays the profile_name -->
    <button id="update_button" action="{% url 'ce_profiles:profile-update-name' profile_name.id %}" class="bold btn btn-main btn-sm button-main">UPDATE</button>
  </div>
  <div id="div_NameForm" class="container">
  </div>
{% endblock %}

{% block script %}
  <script src="{% static 'ce_profiles/ce_profiles.js' %}"></script>

{% endblock %}

当我单击更新按钮时,它应该在要修改的表单中使用 AJAX。我已经尝试将变量替换为“1”,但它仍然不起作用。我有测试来验证视图本身不能与“GET”一起使用,但可以与“POST”一起使用,除了之前的视图。

urls.py

app_name='ce_profiles'
urlpatterns = [
    path('profile-update-name/<int:pk>/', views.profile_update_name,
        name='profile-update-name'
    ),
]

查看

def profile_update_name(request, pk):
    name = get_object_or_404(CEName, id=pk)
    if request.POST:
        name_form = NameForm(data=request.POST, instance=name)
        if name_form.has_changed() == False:
            name_form.add_error(None, _('No changes made.'))
        if name_form.is_valid():
            name_form.save()
            updated_name = get_object_or_404(CEName, id=name.id)
            profile_name_json = render_to_string(
                'ce_profiles/profile_name.html',
                {'profile_name': updated_name,}
            )
            return JsonResponse({'profile_name': profile_name_json})
        else:
          return JsonResponse({'error': name_form.errors})
    else:
        name_form = NameForm(instance=name)
        get_NameForm_json = render_to_string(
            'ce_profiles/update_NameForm.html',
            {'NameForm': name_form}
        )
        return JsonResponse({'NameForm': get_NameForm_json})

这是模板。有什么遗漏吗?

update_NameForm.html

<div id="div_NameForm" class="container">
  <hr size="3px">
  <form id="NameForm" method="POST" action="{% url 'ce_profiles:profile-update-name' profile_name.id %}">
    {% csrf_token %}
    {{ NameForm.as_p }}
    <div id="NameForm_errors"></div>
    <br>
    <button id="save_changes" type="submit" class="btn btn-main button-main btn-block">Save Changes</button>
  </form>
</div>

【问题讨论】:

  • 可能是您的 update_NameForm.html 模板中的一个链接,该链接引用了您的 urls.py 文件中未定义的名称
  • @vctrd 我添加了模板
  • 你能展示视图渲染的模板吗?
  • 你没有将这个变量发送到模板:profile_name,所以profile_name.id 将是 None

标签: django django-views django-urls


【解决方案1】:

您可以使用render(),而不是使用render_to_string,但您忘记将变量profile_name: 发送到模板。

def profile_update_name(request, pk):
    name = get_object_or_404(CEName, id=pk)
    if request.POST:
       ''' code '''

    # no need to use else statement
    # since 'return' breaks the 'if request.POST' statement
    name_form = NameForm(instance=name)
    context = {
        profile_name:name,
        'NameForm': name_form,
    }
    return render(request,'ce_profiles/update_NameForm.html',context) 

请注意,您可以在表单中将action='' 留空,这意味着相同的视图

 <form id="NameForm" method="POST" action="">

【讨论】:

  • 我需要 render_to_string 将我的表单 AJAX 到另一个视图,但 'profile_name': name 完全修复了它。
  • 你必须给我看那个。我很难弄清楚如何将多字段表单 AJAX 到视图并得到响应。有人应该为此制作一个体面的教程。我无法让render() 工作。
  • 这取决于你如何处理js中的响应,响应会有所不同
  • [render_to_string()](https://docs.djangoproject.com/en/2.1/topics/templates/#django.template.loader.render_to_string) 加载类似 get_template() 的模板,因此您可以在 js 中使用 element.html(response) 因为响应只是一个字符串... render() 快捷方式调用 render_to_string() 并将结果输入一个适合从视图返回的 HttpResponse,在 js 中,您可以将响应作为 js 对象进行操作。就像您可以在响应中查询选择器一样。$(response).find('#tag')我没有任何链接抱歉
  • 我现在只是没有把这些点连起来。我想我需要查看它的完整代码。感谢您的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
  • 1970-01-01
相关资源
最近更新 更多