virtualenv中Django服务器运行失败(权限/属性缺失)及页面跳转配置求助
Hey there, let's work through this Django problem step by step! It sounds like you've already knocked out a few bugs (shoutout to fixing the secret key and silencing those system checks), so let's focus on getting your server running and routing to index.html correctly.
First, Fix the URL Configuration Mistakes
The "missing attribute" error is almost certainly coming from incorrect URL setup—let's clean that up:
1. Root URLconf (dasocc_site/urls.py)
You had a typo in the import (dassoc_app instead of dasocc_app) and it's better practice to include your app's URLconf instead of directly referencing views here. Update it to:
from django.urls import path, include urlpatterns = [ path('', include('dasocc_app.urls')), # Delegate root path to your app's URLs ]
2. App URLconf (dasocc_app/urls.py)
Your first path was pointing to a local file path and using include incorrectly—include is for referencing other URLconfs, not views. Simplify it to:
from django.urls import path from . import views urlpatterns = [ path('', views.liga, name="index"), # Map root path directly to your liga view ]
Fix the Template Path in Your View
Django automatically looks for templates in each app's templates directory, so you don't need to use the full file path. Update your views.py:
import requests from django.shortcuts import render def liga(request): liga_list = ['1. Bundesliga', 'Premier League', 'La liga'] # Use the relative path to your template (adjust if your template is in a subfolder) return render(request, 'index.html', {'liga': liga_list}) # If your template is at dasocc_app/templates/dasocc_app/index.html, use 'dasocc_app/index.html' instead
Verify Your Project Structure
Make sure your templates are in the right place—Django expects this structure by default:
dasocc_site/ ├── dasocc_site/ │ ├── settings.py │ ├── urls.py │ └── ... ├── dasocc_app/ │ ├── views.py │ ├── urls.py │ └── templates/ │ └── index.html # Or dasocc_app/index.html if you organized it that way └── manage.py
Fix the Permission Denied Error
If you're still getting permission issues with your virtualenv:
- Linux/macOS: Run this command to make sure your user owns the project and virtualenv directories:
chown -R $USER:$USER /path/to/your/dasocc_project - Windows: Right-click your project folder → Properties → Security → Ensure your user account has "Read & Write" permissions.
Final Steps
- Save all your changes
- Activate your virtualenv
- Restart the server with:
python manage.py runserver
Now visiting http://127.0.0.1:8000/ should route to your index.html template with the liga data passed in.
内容的提问来源于stack exchange,提问作者JSRB

