1、如果在反转url的时候,需要添加参数,那么可以通过传递'kwargs'参数到'reverse'函数中。实例代码:
urls.py
from django.urls import path, re_path from app01 import views urlpatterns = [ path('article/<year>/<month>/',views.article,name='article'), path('', views.Login.as_view(), name="login"), ]
views.py
from django.shortcuts import HttpResponse, redirect, reverse from django.contrib.auth.models import User, from django.views.generic import View from django.contrib.auth import authenticate, login, class Login(View): def get(self, request): return render(request, 'login.html') def post(self, request): username = request.POST.get('username') passwd = request.POST.get('passwd') user = authenticate(request, username=username, password=passwd) if user is not None: if user.is_activ