我得到一个错误.django.urls.exceptions.NoReverseMatch [英] i get an error. django.urls.exceptions.NoReverseMatch

查看:84
本文介绍了我得到一个错误.django.urls.exceptions.NoReverseMatch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几天前我开始学习Django,但出现此错误:

l am started to learn Django few days ago, and I get this error:

django.urls.exceptions.NoReverseMatch:反向查找'create_order',未找到任何参数.尝试了1种模式:['create_order/(?P [^/] +)/$'] *

django.urls.exceptions.NoReverseMatch: Reverse for 'create_order' with no arguments not found. 1 pattern(s) tried: ['create_order/(?P[^/]+)/$']*

urls.py

 path('create_order/<str:pk>/', views.createOrder, name='create_order'),

views.py

def createOrder(request, pk):
customer = Customer.objects.get(id=pk)
form = OrderForm(initial={'customer': customer})
if request.method == 'POST':
    # print('Printing:', request.POST)
    form = OrderForm(request.POST)
    if form.is_valid():
        form.save()
        return redirect('/')
context = {
    'form': form
}
return render(request, 'accounts/order_form.html', context)

order_form.html

{%  extends 'accounts/main.html' %}
{% load static %}
{% block content %}
<br>
<div class="row">
    <div class="col-12 col-md-6">
        <div class="card card-body">
            <form action="" method="post">
                {% csrf_token %}
                {{form}}
                <input class="btn btn-sm btn-danger" type="submit" value="Conform">
            </form>
        </div>
    </div>
</div>

{% endblock %}

customer.html

<div class="row">
	<div class="col-md">
		<div class="card card-body">
			<h5>Customer:</h5>
			<hr>
			<a class="btn btn-outline-info  btn-sm btn-block" href="">Update Customer</a>
			<a class="btn btn-outline-info  btn-sm btn-block" href="{% url 'create_order' customer.id %}">Place Order</a>

		</div>
	</div>

推荐答案

我也遵循了youtube(dennis ivy)的本教程,但遇到了相同的错误,不知道是什么问题,只是将github中的urls.py文件替换为相同的上下文,并且没有显示该错误.

I was also following this tutorial from youtube(dennis ivy) and got the same error, don't know what is the problem but just replace the file urls.py from github with the same context and it's not showing that error,.

urls.py

from django.urls import path
from . import views
urlpatterns = [
    path('', views.home, name="home"),
    path('products/', views.products, name='products'),
    path('customer/<str:pk_test>/', views.customer, name="customer"),

    path('create_order/<str:pk>/', views.createOrder, name="create_order"),
    path('update_order/<str:pk>/', views.updateOrder, name="update_order"),
    path('delete_order/<str:pk>/', views.deleteOrder, name="delete_order"),


]

views.py

from django.forms import inlineformset_factory
def createOrder(request, pk):
    OrderFormSet = inlineformset_factory(Customer, Order, fields=('product', 'status'), extra=10 )
    customer = Customer.objects.get(id=pk)
    formset = OrderFormSet(queryset=Order.objects.none(),instance=customer)
    #form = OrderForm(initial={'customer':customer})
    if request.method == 'POST':
        #print('Printing POST:', request.POST)
        #form = OrderForm(request.POST)
        formset = OrderFormSet(request.POST, instance=customer)
        if formset.is_valid():
            formset.save()
            return redirect('/')

    context = {'form':formset}
    return render(request, 'accounts/order_form.html', context)

order_form.html

{%  extends 'accounts/main.html' %}
{% load static %}
{% block content %}


<div class="row">
    <div class="col-md-6">
        <div class="card card-body">

            <form action="" method="POST">
                {% csrf_token %}
                {{ form.management_form }}
                {% for field in form %}
                    {{field}}
                    <hr>
                {% endfor %}

                <input type="submit" name="Submit">
            </form>

        </div>
    </div>
</div>


{% endblock %}

再次

我不知道为什么会显示此错误以及问题出在哪里,但只是用github上的相同代码重新填充了它,它就起作用了..如果有人知道它是如何起作用的,那在不久的将来会很有帮助.感谢一切,哈里斯·艾哈迈德(Haris Ahmad)

again I don't know why it was showing this error and where was the problem but just relapced it with the same code from github and it worked..if someone know how it worked , that will be really helpful in near future. thanks to all regards Haris Ahmad

这篇关于我得到一个错误.django.urls.exceptions.NoReverseMatch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆