这是我的车urls.py ::
from django.urls import path
from . import views
app_name = 'cars'
urlpatterns = [
path('', views.CarsListView.as_view(), name='cars'),
path('add-car/', views.AddCarView.as_view(), name='add_car'),
path('car/', views.RepairsListView.as_view(), name='car_detail'),
path('car/<int:pk>/update/', views.UpdateCarView.as_view(), name='update_car'),
path('car/<int:pk>/delete/', views.DeleteCarView.as_view(), name='delete_car'),
path('car/<int:pk>/new-repair/', views.AddRepairView.as_view(), name='add_repair'),
]
这是我的车views.py ::
from .models import Car, Repair
from django.views.generic import ListView, UpdateView, DeleteView, CreateView
from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin
from django.urls import reverse_lazy
from django.contrib import messages
class CarsListView(LoginRequiredMixin, ListView):
model = Car
template_name = 'cars/cars.html'
context_object_name = 'cars'
paginate_by = 10
def get_queryset(self):
if self.request.GET.get('q'):
q = self.request.GET.get('q')
make_results = self.model.objects.filter(
user=self.request.user, make=q).order_by('-pk')
model_results = self.model.objects.filter(
user=self.request.user, model=q).order_by('-pk')
if make_results.exists():
return make_results
elif model_results.exists():
return model_results
else:
return self.model.objects.none()
return self.model.objects.filter(user=self.request.user).order_by('-pk')
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['q'] = self.request.GET.get('q', '')
return context
class AddCarView(LoginRequiredMixin, CreateView):
model = Car
fields = ['make', 'model', 'vrn', 'year']
success_url = '/'
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class DeleteCarView(LoginRequiredMixin, UserPassesTestMixin, DeleteView):
model = Car
success_url = '/'
def test_func(self):
if self.get_object().user == self.request.user:
return True
return False
def delete(self, request, *args, **kwargs):
success_message = f'Car {self.get_object()} has been deleted'
messages.success(self.request, success_message)
return super().delete(request, *args, **kwargs)
class UpdateCarView(LoginRequiredMixin, UserPassesTestMixin, UpdateView):
model = Car
fields = ['make', 'model', 'vrn', 'year']
success_message = 'Car info has been updated'
def get_success_url(self, **kwargs):
row = self.request.GET.get('row')
p = self.request.GET.get('p')
q = self.request.GET.get('q')
options = '?p=' + p + '&row=' + row
options += '&q=' + q
messages.success(self.request, self.success_message)
return reverse_lazy('car_detail') + options
def test_func(self):
if self.get_object().user == self.request.user:
return True
return False
class AddRepairView(LoginRequiredMixin, CreateView):
model = Repair
fields = ['date', 'description']
success_message = 'New repair has been added'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['car'] = Car.objects.get(id=self.kwargs['pk'])
return context
def form_valid(self, form, **kwargs):
form.instance.car = Car.objects.get(id=self.kwargs['pk'])
return super().form_valid(form)
def get_success_url(self, **kwargs):
row = self.request.GET.get('row')
p = self.request.GET.get('p')
q = self.request.GET.get('q')
options = '?p=' + p + '&row=' + row
options += '&q=' + q
return reverse_lazy('car_detail') + options
class RepairsListView(LoginRequiredMixin, ListView):
model = Repair
paginate_by = 10
def __get_car(self):
cars = Car.objects.filter(user=self.request.user).order_by('-pk')
if self.request.GET.get('q'):
q = self.request.GET.get('q')
make_results = Car.objects.filter(
user=self.request.user, make=q).order_by('-pk')
model_results = Car.objects.filter(
user=self.request.user, model=q).order_by('-pk')
if make_results.exists():
cars = make_results
elif model_results.exists():
cars = model_results
id = int(self.request.GET.get('row')) - 1
page = int(self.request.GET.get('p')) - 1
id = id + page * 10
return cars[id]
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['car'] = self.__get_car()
context['row'] = self.request.GET.get('row')
context['p'] = self.request.GET.get('p')
context['q'] = self.request.GET.get('q')
return context
def get_queryset(self, **kwargs):
return self.model.objects.filter(car=self.__get_car()).order_by('-pk')
这是我的用户views.py ::
from django.shortcuts import render, redirect
from django.contrib.auth.forms import UserCreationForm, PasswordChangeForm
from django.contrib.auth import login, authenticate
from django.views.generic import CreateView
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
class RegisterUser(CreateView):
form_class = UserCreationForm
def get(self, request, *args, **kwargs):
return render(request, 'users/register-user.html', {'form': UserCreationForm()})
def post(self, request, *args, **kwargs):
form = UserCreationForm(request.POST)
if form.is_valid():
form.save()
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password1')
user = authenticate(username=username, password=password)
login(request, user)
messages.success(request, f'Welcome, {username}!')
return redirect('cars')
return render(request, 'users/register-user.html', {'form': form})
class ChangePassword(LoginRequiredMixin, CreateView):
form_class = PasswordChangeForm
template_name = 'users/change-password'
def get(self, request, *args, **kwargs):
form = PasswordChangeForm(request.user)
return render(request, 'users/change-password.html', {'form': form})
def post(self, request, *args, **kwargs):
form = PasswordChangeForm(request.user, request.POST)
if form.is_valid():
user = form.save()
update_session_auth_hash(request, user)
messages.success(request, 'Password changed!')
return redirect('cars')
return render(request, 'users/change-password.html', {'form': form})
当我要进入汽车页面时,出现错误:::((/ cars /中的NoReverseMatch) 和 (未找到与“登录”相反的名称。“登录”不是有效的视图函数或模式名称。)
有人可以帮助我吗? 请帮助我!!!!!!
答案 0 :(得分:0)
尝试将您的汽车和用户网址包含在基本urls.py中。
from yourproject.cars import urls as car_urls
from yourproject.users import urls as users_urls
,然后将以下内容添加到您的基本网址模式
url(r"^cars/", include((car_urls)),
url(r"^users/", include((user_urls)),