为什么在相似的Django数据库查询不起作用的情况下一个Django数据库查询仍然起作用

时间:2019-06-30 12:17:09

标签: django database forms

我重复使用了两次类似的代码,但有一次它没有向数据库添加任何内容,有人可以告诉我为什么吗?问题是缺少表单验证?

我试图每次将表单发送到服务器时,将某些用户模型字段增加某些整数。其中一个工作,一个没有工作。

下面的代码每次都会使amount_comments增加一:

def add_comment(request, pk):
    ticket = get_object_or_404(Ticket, pk=pk)
    if request.method == "POST":
        form = CommentForm(request.POST)
        if form.is_valid():
            comment = form.save(commit=False)
            comment.ticket = ticket
            comment.author = request.user
            comment.save()
            user = CustomUser.objects.get(username=request.user)
            user.amount_comments += 1
            user.save()
            messages.success(request, ('Comment added!'))
            return redirect('show_single', pk=pk)
    else:
        form = CommentForm()
    return render(request, 'add_comment.html', {'form': form})

...并且由于某种原因,此人未增加捐款:

def show(request, pk):
    if request.method == "POST":
        user = CustomUser.objects.get(username=request.user)
        user.contributions += 5
        user.save()
        return redirect('checkout', pk=pk)
    ticket = Ticket.objects.get(pk=pk)
    comment_list = Comment.objects.select_related().filter(
                   ticket_id=pk).order_by('-date')
    paginator = Paginator(comment_list, 4)
    page = request.GET.get('page')
    comments = paginator.get_page(page)
    key = settings.STRIPE_PUBLISHABLE_KEY
    return render(request, 'single_ticket.html', {'ticket': ticket, 'comments':
                  comments, 'key': key})

我每次检查管理面板都不会出错,并且每次amount_comments上升时,用户的贡献字段也不会更改为5。

CustomUser扩展AbstractUser并添加了两个字段:

from django.db import models
from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
    contributions = models.IntegerField(default='0')
    amount_comments = models.IntegerField(default='0')

第二次checkout.js是用于表单的库:

<form action="{% url 'checkout' ticket.id %}" method="post">
    {% csrf_token %}
    <script src="https://checkout.stripe.com/checkout.js" 
    class="stripe-button"
    data-key={{ key }}
    data-description="Payment"
    data-amount="500"
    data-currency="gbp"
    data-locale="auto">
    </script>
</form>

urls.py:

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

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.home, name='home'),
    path('blog/', include('blog.urls')),
    path('ticket/', include('ticket.urls')),
    path('checkout/', include('checkout.urls')),
    path('login/', views.login_user, name='login'),
    path('sign_up', views.sign_up, name='sign_up'),
    path('logout/', views.logout_user, name='logout'),
    path('all_tickets/', views.all_tickets, name='all_tickets')
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

结帐应用urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('<int:pk>', views.checkout, name='checkout'),
]

门票应用urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('delete/<ticket_id>', views.delete, name='delete'),
    path('show/<int:pk>', views.show, name='show_single'),
    path('show/<int:pk>/comment/', views.add_comment, name='add_comment'),
    path('add', views.add_ticket, name='add_ticket'),
]

1 个答案:

答案 0 :(得分:1)

您要发布到{% url 'checkout' ticket.id %},该链接在此处path('<int:pk>', views.checkout, name='checkout')链接到views.checkout,而不是show。因此,该代码(show)从未针对给定的形式执行。将此模板标记中的URL模式名称更改为正确的名称:

{% url 'show_single' ticket.id %}