生产环境项目中是否可同时使用DRF APIView与Django Views?电商项目结构该如何设计?
Absolutely, you can absolutely use Django's regular Views alongside DRF's APIView—they’re designed to work together, and in many real-world projects (like e-commerce), this is actually the norm!
Let me break this down for you:
1. Why mixing both views makes sense
- Regular Django Views are perfect for rendering HTML templates: think customer-facing product pages, user login/signup forms, admin dashboards with interactive UIs. They handle server-side rendering and traditional web requests.
- DRF's APIView (and other DRF view classes) are built for creating RESTful APIs: they return JSON/XML responses, handle authentication/authorization out of the box, and are ideal for powering mobile apps, single-page apps (SPAs), or any client that needs structured data rather than rendered HTML.
There’s no rule saying you have to pick one or the other—use each tool for what it’s best at.
2. A practical e-commerce project structure (skip the standalone "api" app)
Your friend’s suggestion of a separate api app might seem clean at first, but it often leads to scattered logic (e.g., product models in products app, product APIs in api app) which becomes a pain to maintain as your project grows.
Instead, organize your app by business domain (not by view type), and nest API logic within each domain-specific app. Here’s a typical structure:
ecommerce_project/ ├── ecommerce_project/ # Project config root │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py ├── core/ # Shared utilities & base code │ ├── models.py # Base models (e.g., TimestampedModel) │ ├── utils.py # Helper functions (e.g., currency formatting) │ └── views.py # Generic template views ├── users/ │ ├── models.py # User, Customer, Seller models │ ├── views.py # Login/signup template views, profile pages │ ├── api/ │ │ ├── views.py # User auth APIs, profile data APIs (APIView-based) │ │ └── urls.py # API sub-routes │ └── urls.py # Regular view routes ├── products/ │ ├── models.py # Product, Category, Inventory models │ ├── views.py # Product listing/detail pages, search results │ ├── api/ │ │ ├── views.py # Product list/detail APIs, inventory update APIs │ │ └── urls.py │ └── urls.py ├── orders/ │ ├── models.py # Order, OrderItem, Payment models │ ├── views.py # Cart page, checkout flow, order history pages │ ├── api/ │ │ ├── views.py # Cart management APIs, order creation/query APIs │ │ └── urls.py │ └── urls.py ├── dashboard/ # Admin/merchant backend │ ├── views.py # Sales stats, product management UI │ ├── api/ │ │ ├── views.py # Admin-only APIs (e.g., bulk product status updates) │ │ └── urls.py │ └── urls.py └── manage.py
3. Key tips for this structure
- Route separation: In your project’s main
urls.py, split regular and API routes for clarity:# Regular web routes path('', include('users.urls')), path('products/', include('products.urls')), path('dashboard/', include('dashboard.urls')), # API routes path('api/v1/users/', include('users.api.urls')), path('api/v1/products/', include('products.api.urls')), path('api/v1/dashboard/', include('dashboard.api.urls')), - Reuse business logic: Avoid duplicating code between regular views and APIs. Create a
services.pyfile in each app (e.g.,products/services.py) to hold core logic like fetching product details, then call that from both your template view and APIView. - Permissions: Use DRF’s permission classes (like
IsAdminUser,IsAuthenticated) for API endpoints that need admin access, and Django’s@login_requireddecorator for regular admin views. This keeps your admin-only functionality secure across both view types.
Final thought
Your goal should be to keep related code together—so product models, views, and APIs all live in the products app, not split across multiple apps. This makes your project easier to navigate, maintain, and scale as your e-commerce store grows.
内容的提问来源于stack exchange,提问作者Reza Jeffrey

