如何在Django,Python中修复'NoReverseMatch'错误

时间:2019-09-18 06:58:15

标签: python django

我收到此错误。

  

在/ reservation /处对NoreverseMatch进行'reservation_remove'反向   找不到参数'('',)'。尝试了1种模式:   ['reservation / reservation_remove /(?P [0-9] +)/ $']

我不知道该怎么解决。

这是我的预订应用程序的urls.py。

from django.urls import path
from .import views

app_name='reservation'

urlpatterns = [
     path('add/<int:nursery_id>/', views.add_reservation, name='add_reservation'),
     path('', views.reservation_detail, name='reservation_detail'), 
     path('reservation_remove/<int:nursery_id>/', views.reservation_remove, name='reservation_remove'),
     path('full_remove/<int:nursery_id>/', views.full_remove, name='full_remove'),
]

这是我的views.py

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import City,Nursery
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.contrib.auth.models import Group, User
from .forms import SignUpForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, authenticate, logout


def index(request):
    text_var = 'Start!!!'
    return HttpResponse(text_var)


def allCity(request, c_slug=None):
    c_page = None
    cities_list = None
    if c_slug!=None:
        c_page = get_object_or_404(City,slug=c_slug)
        cities_list = City.objects.filter(city=c_page)
    else:
        cities_list = City.objects.all()
    paginator = Paginator(cities_list, 6)
    try:
        page = int(request.GET.get('page','1'))
    except:
        page = 1
    try:
        cities = paginator.page(page)
    except (EmptyPage,InvalidPage):
        cities = paginator.page(paginator.num_pages)  


    return render(request,'service/category.html',{'cities':cities})



def allSitting(request, c_slug=None):
    c_page = None
    sittings_list = None
    if c_slug!=None:
        c_page = get_object_or_404(City,slug=c_slug)
        sittings_list = Nursery.objects.filter(city=c_page,available=True)
    else:
        sittings_list = Nursery.objects.all().filter(available=True)
    paginator = Paginator(sittings_list, 6)
    try:
        page = int(request.GET.get('page','1'))
    except:
        page = 1
    try:
        sittings = paginator.page(page)
    except (EmptyPage,InvalidPage):
        sittings = paginator.page(paginator.num_pages)    
    return render(request,'service/city.html',{'city':c_page,'sittings':sittings})

    def SittingDetail(request, c_slug, nursery_slug):
        try:
            nursery = Nursery.objects.get(city__slug=c_slug, slug=nursery_slug)
        except Exception as e:
            raise e
    return render(request,'service/nursery.html', {'nursery':nursery})

def signupView(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            signup_user = User.objects.get(username=username)
            customer_group = Group.objects.get(name='Customer')
            customer_group.user_set.add(signup_user)
    else:
        form = SignUpForm()
    return render(request, 'accounts/signup.html', {'form':form})

def signinView(request):
   if request.method == 'POST':
       form = AuthenticationForm(data=request.POST)
       if form.is_valid():
           username = request.POST['username']
           password = request.POST['password']
           user = authenticate(username=username, password=password)
           if user is not None:
               login(request, user)
               return redirect('service:allSitting')
           else:
               return redirect('signup')
   else:
       form = AuthenticationForm()
   return render(request,'accounts/signin.html', {'form':form })

def signoutView(request):
   logout(request)
   return redirect('signin')

这是我的项目的urls.py。

from django.contrib import admin
from django.urls import path, include, re_path
from service import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.allCity, name='allCity'),
    path('service/', include('service.urls')),
    path('search/', include('search_app.urls')),
    path('reservation/', include('reservation.urls')),
    path('order/', include('order.urls')),
    path('account/create/', views.signupView, name='signup'),
    path('account/login/', views.signinView, name='signin'),
    path('account/logout/', views.signoutView, name='signout'),
 ]

if settings.DEBUG:
    urlpatterns += 
static(settings.STATIC_URL,document_root=settings.STATIC_ROOT)
    urlpatterns += 
static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

这是我的预订应用程序模板。

{% extends "base.html" %}
{% load staticfiles %}
{% block metadescription %}
    This is the reservation page. Proceed to review your items and place the reservation perfectry.
{% endblock %}
{% block title %}
    Reservation - Travel Sitter
{% endblock %}
{% block content %}
    {% if not reservation_items %}
        <div>
            <div class="text-center">
                <br>
                <h1 class="text-center my_title">
                    You don't have a resevation.
                </h1>
                <br>
                <p class="text-center">
                    Please click <a href="{% url 'service:allReservation' %}">here</a> to make the other reservation.
                </p>
            </div>
        </div>
    {% else %}
        <div>
            <div class="text-center">
                <br>
                <h1 class="text-center my_title">
                    Your reservation
                </h1>
                <br>
             </div>
        </div>
        <div class="row mx-auto">
            <div class="col-12 col-sm-12 col-md-12 col-lg-6 text-center">
                <table class="table my_custom_table">
                    <thead class="my_custom_thead">
                        <tr>
                            <th colspan="5">
                                Your reservation
                            </th>
                        </tr>   
                    </thead>
                    <tbody>
                        {% for resesrvation_item in reservation_items %}
                            <tr>
                                <td>
                                    <a href="{{reservation_item.nursery.get_url}}"><img src="{{reservation_item.nursery.image.url}}" alt="" class="float-left rounded custom_image"></a>
                                </td>
                                <td class="text-left">
                                    {{reservation_item.nursery.name}} 
                                    <br>
                                    SKU: {{reservation_item.nursery.id}}
                                    <br>
                                    Price: ${{reservation_item.nursery.price}}
                                    <br>
                                    Total: {{reservation_item.quantity}} × ${{reservation_item.nursery.price}}
                                 </td>
                                 <td>
                                     ${{cart_item.sub_total}}
                                 </td>
                                 {% if reservation_item.quantity < reservation_item.nursery.stock %}
                                     <td>
                                         <a href="{% url 'reservation:add_reservation' reservation_item.nursery.id %}" class="custom_a"><i class="fas fa-plus-circle custom_icon"></i></a>&nbsp;<a href="{% url 'reservation:reservation_remove' reservation_item.nursery.id %}" class="custom_a"><i class="fas fa-minus-circle custom_icon"></i></a>&nbsp;<a href="{% url 'reservation:full_remove' reservation_item.nursery.id %}" class="custom_icon"><i class="fas fa-trash-alt custom_icon"></i></a>
                                     </td>    
                                {% else %}
                                     <td>
                                         &nbsp;<a href="{% url 'reservation:reservation_remove' reservation_item.nursery.id %}" class="custom_a"><i class="fas fa-minus-circle custom_icon"></i></a>&nbsp;<a href="{% url 'reservation:full_remove' reservation_item.nursery.id %}" class="custom_icon"><i class="fas fa-trash-alt custom_icon"></i></a>
                                     </td>
                                     <td></td>
                                {% endif %}
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>

            </div>
            <div class="col-12 col-sm-12 col-md-12 col-lg-6 text-center">
                <table class="table my_custom_table">
                    <thead class="my_custom_thead">
                        <tr>
                            <th>
                                Check out
                            </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td>
                                Please review your reservation before proceeding the payment.
                            </td> 
                        </tr>
                        <tr>
                            <td class="text-left">
                                Your total is: <strong>${{ total }}</strong>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <div class="mx-auto">
                    <form action="" method="POST">
                        {% csrf_token %}
                      <script
                        src="https://checkout.stripe.com/checkout.js" class="stripe-button"
                        data-key="{{ data_key }}"
                        data-image="{% static 'img/atom.png' %}"
                        data-name="Travel Sitter"
                        data-description="{{ description }}"
                        data-amount="{{ stripe_total }}"
                        data-locale="auto"
                        data-currency="usd"
                        data-shipping-address="true"
                        data-billing-address="true"
                        data-zip-code="true">
                      </script>
                    </form>
                     <a href="{% url 'service:allReservation' %}" class="btn btn-secondary btn-block my_custom_button">Make the other Reservation</a>
                </div>
            </div>
        </div>
        <br>
    {% endif %}
{% endblock %}

这是我的预订视图的Reservation_remove功能。

 def reservation_remove(request, nursery_id):
    reservation = 
Reservation.objects.get(reservation_id=_reservation_id(request))
    nursery = get_object_or_404(Nursery, id=nursery_id)
    reservation_item = ReservationItem.objects.get(nursery=nursery, 
reservation=reservation)
    if reservation_item.quantity > 1:
        reservation_item.quantity -= 1
        reservation_item.save()
    else:
        reservation_item.delete()
    return redirect('reservation:reservation_detail')  

这是我对应用程序的views.py。

from django.shortcuts import render, get_object_or_404, redirect
from django.http import HttpResponse
from .models import City,Nursery
from django.core.paginator import Paginator, EmptyPage, InvalidPage
from django.contrib.auth.models import Group, User
from .forms import SignUpForm
from django.contrib.auth.forms import AuthenticationForm
from django.contrib.auth import login, authenticate, logout


def index(request):
    text_var = 'Start!!!'
    return HttpResponse(text_var)


def allCity(request, c_slug=None):
    c_page = None
    cities_list = None
    if c_slug!=None:
        c_page = get_object_or_404(City,slug=c_slug)
        cities_list = City.objects.filter(city=c_page)
    else:
        cities_list = City.objects.all()
    paginator = Paginator(cities_list, 6)
    try:
        page = int(request.GET.get('page','1'))
    except:
        page = 1
    try:
        cities = paginator.page(page)
    except (EmptyPage,InvalidPage):
        cities = paginator.page(paginator.num_pages)  


    return render(request,'service/category.html',{'cities':cities})



def allSitting(request, c_slug=None):
    c_page = None
    sittings_list = None
    if c_slug!=None:
        c_page = get_object_or_404(City,slug=c_slug)
        sittings_list = Nursery.objects.filter(city=c_page,available=True)
    else:
        sittings_list = Nursery.objects.all().filter(available=True)
    paginator = Paginator(sittings_list, 6)
    try:
        page = int(request.GET.get('page','1'))
    except:
        page = 1
    try:
        sittings = paginator.page(page)
    except (EmptyPage,InvalidPage):
        sittings = paginator.page(paginator.num_pages)    
    return render(request,'service/city.html',{'city':c_page, 
 'sittings':sittings})

def SittingDetail(request, c_slug, nursery_slug):
    try:
        nursery = Nursery.objects.get(city__slug=c_slug, slug=nursery_slug)
    except Exception as e:
        raise e
    return render(request,'service/nursery.html', {'nursery':nursery})

def signupView(request):
    if request.method == 'POST':
        form = SignUpForm(request.POST)
        if form.is_valid():
            form.save()
            username = form.cleaned_data.get('username')
            signup_user = User.objects.get(username=username)
            customer_group = Group.objects.get(name='Customer')
            customer_group.user_set.add(signup_user)
    else:
        form = SignUpForm()
    return render(request, 'accounts/signup.html', {'form':form})

def signinView(request):
   if request.method == 'POST':
       form = AuthenticationForm(data=request.POST)
       if form.is_valid():
           username = request.POST['username']
           password = request.POST['password']
           user = authenticate(username=username, password=password)
           if user is not None:
               login(request, user)
               return redirect('service:allSitting')
           else:
               return redirect('signup')
   else:
      form = AuthenticationForm()
   return render(request,'accounts/signin.html', {'form':form })

def signoutView(request):
   logout(request)
   return redirect('signin')

如果我必须显示其他代码,请教我。

0 个答案:

没有答案