无法从购物车页面-Django URL错误访问我的网上商店的订单页面

时间:2018-11-08 21:24:52

标签: python django

在Django 2.0.5中正在做网上商店示例,尝试将购物车检出到订单页面时遇到了URL错误。它将错误带到下面

找不到页面(404) 请求方法:GET 要求网址:http://127.0.0.1:8000/cart/%25%20url Django使用myshop.urls中定义的URLconf,按以下顺序尝试了以下URL模式:

admin/
orders/
cart/ [name='cart_detail']
cart/ add/<int:product_id>/ [name='cart_add']
cart/ remove/<int:product_id>/ [name='cart_remove']
[name='product_list']
<slug:category_slug>/ [name='product_list_by_category']
<int:id>/<slug:slug>/ [name='product_detail']
^media\/(?P<path>.*)$

当前路径,购物车/%网址,与任何这些都不匹配。

myshop / orders / urls.py

from django.urls import path
from . import views
app_name = 'orders'
urlpatterns = [
    path('create/', views.order_create, name='order_create'),
]

myshop / orders / views.py

from .models import Order
from django.conf import settings
from django.http import HttpResponse
def order_create(request):
    cart = Cart(request)
    if request.method == 'POST':
        form = OrderCreateForm(request.POST)
        if form.is_valid():
            order = form.save()
            for item in cart:
                OrderItem.objects.create(order=order,
                                         product=item['product'],
                                         price=item['price'],
                                         quantity=item['quantity'])
            # clear the cart
            cart.clear()
            return render(request,
                          'orders/order/created.html',
                          {'order': order})
    else:
        form = OrderCreateForm()
    return render(request,
                  'orders/order/create.html',
                  {'cart': cart, 'form': form})

myshop / orders / admin.py

from django.contrib import admin
from django.http import HttpResponse
from .models import Order, OrderItem
class OrderItemInline(admin.TabularInline):
    model = OrderItem
    raw_id_fields = ['product']
@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ['id', 'first_name', 'last_name', 'email',
                    'address', 'postal_code', 'city', 'paid',
                    'created', 'updated']
    list_filter = ['paid', 'created', 'updated']
    inlines = [OrderItemInline]

myshop / cart / urls.py

from django.urls import path
from . import views
app_name = 'cart'
urlpatterns = [
    path('', views.cart_detail, name='cart_detail'),
    path('add/<int:product_id>/', views.cart_add, name='cart_add'),
    path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),  
]

myshop / myshop / urls.py

from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
    path('admin/', admin.site.urls),
    path('orders/', include('orders.urls', namespace='orders')),
    path('cart/', include('cart.urls', namespace='cart')),
    path('', include('shop.urls', namespace='shop')),
]
if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL,
                          document_root=settings.MEDIA_ROOT)

myshop / cart / templates / cart / detail.html

</table>
 <p class="text-right">
  <a href="{% url "shop:product_list" %}" class="buttonlight">Continue 
shopping</a>
 <a href="% url "orders:order_create" %}" class="button">Checkout</a>
 </p>
{% endblock %}

例如,我正在做《 django 2》这本书,请help.am卡住

1 个答案:

答案 0 :(得分:0)

尝试

嗨, 在detail.html中出错:

href="{% url "shop:product_list" %}"

如果使用外部双引号,则使用单引号或双引号,然后使用内部单引号,然后应无错误显示: 试试吧:

href="{% url 'shop:product_list' %}"