在多个app下有相同函数时,可能会出现混乱,这时要给每个app取名

基本目录:

 settings.py

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'book',
    'news',
]

book/views.py

from django.http import HttpResponse
from django.shortcuts import render,redirect,reverse

# Create your views here.
def index(request):
    username = request.GET.get("username")
    if username is not None:
        return HttpResponse("welcome!")
    else:
        return redirect(reverse('book:loose',kwargs={'a':100,'b':200}))

def error(request,a,b):
    sum=a+b
    return HttpResponse("<h1>path:{}</h1>".format(request.path))

news/views.py

from django.http import HttpResponse
from django.shortcuts import render,redirect,reverse

# Create your views here.
def index(request):
    username = request.GET.get("username")
    if username is not None:
        return HttpResponse("welcome!")
    else:
        return redirect(reverse('news:loose',kwargs={'a':100,'b':200}))

def error(request,a,b):
    sum=a+b
    return HttpResponse("<h1>path:{}</h1>".format(request.path))

book/urls.py

from django.urls import path
from . import views
app_name ="book"
urlpatterns = [
    path('', views.index,name='index'),
    path('error/<int:a>/<int:b>', views.error,name='loose'),
]

news/urls.py

from django.urls import path
from . import views
app_name ="news"
urlpatterns = [
    path('', views.index,name='index'),
    path('error/<int:a>/<int:b>', views.error,name='loose'),
]

newwebsite/urls.py

from django.contrib import admin
from django.urls import path,include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('book/',include('book.urls')),
    path('news/',include('news.urls')),
]
01-02 02:32