我正在做一个django项目来创建足球训练。我正在测试模型和它的保存。当我试图保存它的时候,问题出现了,一定是关于重定向的,因为在数据库中训练确实被保存了,但是它没有把我重定向到任何地方,错误出来了。
models.py
from django.db import models
from django.utils import timezone
from users.models import User
class Entrenamiento(models.Model):
autor = models.ForeignKey(User, on_delete=models.CASCADE)
idEntrenamiento = models.AutoField(primary_key=True)
idEquipo = models.IntegerField()
fecha = models.DateTimeField(default=timezone.now)
idDireccionCampo = models.IntegerField()
temporadas = [
('2022/2023','2022/2023'),
('2023/2024','2023/2024'),
('2024/2025','2024/2025'),
('2025/2026','2025/2026')
]
temporada = models.CharField(max_length=50, choices=temporadas, default='2022/2023')
def __str__(self):
return 'Entrenamiento {}'.format(self.idEntrenamiento)
@property
def entreno(self):
return 'Entrenamiento {} de {} para {} con fecha del {}, será en el campo {}'.format(self.idEntrenamiento, self.temporada, self.idEquipo, self.fecha, self.idDireccionCampo)
views.py
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.contrib.messages.views import SuccessMessageMixin
from django.contrib.auth.models import User
from django.contrib import messages
from django.http import HttpRequest, HttpResponseRedirect
from django.shortcuts import get_object_or_404, render
from django.views.generic import (CreateView, DeleteView, DetailView, ListView,UpdateView)
from django import forms
from django.urls import reverse_lazy
from .models import Entrenamiento
def home(request):
context = {
'entrenamientos': Entrenamiento.objects.all()
}
return render(request, 'entrenamientos/entrenamientos.html', context)
class PostListView(ListView):
model = Entrenamiento
template_name = 'entrenamientos/entrenamientos.html'#<app>/<model>_<viewtype>.html
context_object_name = 'entrenamientos'
ordering = ['-fecha']
class PostDetailView(DetailView):
model = Entrenamiento
class PostCreateView(LoginRequiredMixin, CreateView):
model = Entrenamiento
fields = ['autor','idEntrenamiento','idEquipo','idDireccionCampo','fecha']
widgets = {'fecha': forms.DateInput(attrs={'type': 'date'})}
def form_valid(self,form):
form.instance.autor = self.request.user
return super().form_valid(form)
class PostUpdateView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Entrenamiento
fields = ['autor','idEntrenamiento','idEquipo']
def form_valid(self,form):
form.instance.autor = self.request.user
return super().form_valid(form)
def test_func(self):
entrenamiento = self.get_object()
if self.request.user == entrenamiento.autor:
return True
return False
class PostDeleteView(DeleteView):
model = Entrenamiento
success_url = '/'
def test_func(self):
entrenamiento = self.get_object()
if self.request.user == entrenamiento.autor:
return True
return False
urls.py
from django.urls import path
from .views import PostListView, PostDetailView, PostCreateView, PostUpdateView, PostDeleteView
from django.contrib.auth.decorators import login_required
app_name = 'entrenamientos'
urlpatterns = [
path('', login_required(PostListView.as_view()), name='entrenamientos'),
path('entrenamiento/<int:pk>/',login_required( PostDetailView.as_view()), name='entrenamiento-detail'),
path('entrenamiento/new/',login_required( PostCreateView.as_view()), name='entrenamiento-create'),
path('entrenamiento/<int:pk>/update/',login_required( PostUpdateView.as_view()), name='entrenamiento-update'),
path('entrenamiento/<int:pk>/delete/',login_required( PostDeleteView.as_view()), name='entrenamiento-delete'),
path('entrenamiento', login_required(PostListView.as_view()), name='entrenamientos')
]
错误:
在/entrenamientos/entrenamiento/new处配置不正确/没有要重定向到的URL。请提供URL或在模型上定义get_absolute_url方法。
我觉得有些地方不对劲:定义表单有效(自身,表单):
我尝试在def form_valid中添加此内容,但出现了另一个错误
# success_url = reverse_lazy('entrenamiento')
# success_message = 'Entrenamiento añadido'
我希望解决方案
1条答案
按热度按时间ecfsfe2w1#
你必须在类视图中提供
success_url
。你可以在其中一种可能性中做到这一点,但第一种就足够了:同时查看您的网址:
您设置了两个完全相同名称的url。您不应该这样做。第一个url总是使用
{% url %}
标记呈现。您还需要第二个url吗?