基于django的图书二手交易系统代码实现
时间: 2023-07-26 11:11:27 浏览: 274
基于 vue + django 的二手图书管理系统源码.zip
5星 · 资源好评率100%
以下是一个基于Django的图书二手交易系统的代码实现示例,仅供参考:
1. 数据库模型
```python
from django.db import models
from django.contrib.auth.models import User
class Book(models.Model):
title = models.CharField(max_length=200)
author = models.CharField(max_length=200)
publisher = models.CharField(max_length=200)
description = models.TextField()
price = models.DecimalField(max_digits=6, decimal_places=2)
image = models.ImageField(upload_to='book_covers/')
owner = models.ForeignKey(User, on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
class Order(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
buyer = models.ForeignKey(User, on_delete=models.CASCADE)
quantity = models.IntegerField()
total_price = models.DecimalField(max_digits=6, decimal_places=2)
address = models.CharField(max_length=200)
payment_method = models.CharField(max_length=200)
created_at = models.DateTimeField(auto_now_add=True)
```
2. 表单
```python
from django import forms
from .models import Book, Order
class BookForm(forms.ModelForm):
class Meta:
model = Book
fields = ('title', 'author', 'publisher', 'description', 'price', 'image')
class OrderForm(forms.ModelForm):
class Meta:
model = Order
fields = ('quantity', 'address', 'payment_method')
```
3. 视图函数
```python
from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Book, Order
from .forms import BookForm, OrderForm
@login_required
def sell_book(request):
if request.method == 'POST':
form = BookForm(request.POST, request.FILES)
if form.is_valid():
book = form.save(commit=False)
book.owner = request.user
book.save()
return redirect('book_detail', book_id=book.id)
else:
form = BookForm()
return render(request, 'sell_book.html', {'form': form})
def book_list(request):
books = Book.objects.all()
return render(request, 'book_list.html', {'books': books})
def book_detail(request, book_id):
book = get_object_or_404(Book, pk=book_id)
return render(request, 'book_detail.html', {'book': book})
@login_required
def add_to_cart(request, book_id):
book = get_object_or_404(Book, pk=book_id)
if request.method == 'POST':
form = OrderForm(request.POST)
if form.is_valid():
order = form.save(commit=False)
order.book = book
order.buyer = request.user
order.total_price = order.quantity * book.price
order.save()
return redirect('cart')
else:
form = OrderForm()
return render(request, 'add_to_cart.html', {'book': book, 'form': form})
@login_required
def cart(request):
orders = Order.objects.filter(buyer=request.user)
return render(request, 'cart.html', {'orders': orders})
```
4. URL配置
```python
from django.urls import path
from . import views
urlpatterns = [
path('sell_book/', views.sell_book, name='sell_book'),
path('book_list/', views.book_list, name='book_list'),
path('book_detail/<int:book_id>/', views.book_detail, name='book_detail'),
path('add_to_cart/<int:book_id>/', views.add_to_cart, name='add_to_cart'),
path('cart/', views.cart, name='cart'),
]
```
5. 模板
sell_book.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>Sell a Book</h2>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}
```
book_list.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>Book List</h2>
<ul>
{% for book in books %}
<li>
<a href="{% url 'book_detail' book.id %}">
{{ book.title }} by {{ book.author }}
</a>
- {{ book.price }}
<a href="{% url 'add_to_cart' book.id %}">Add to Cart</a>
</li>
{% endfor %}
</ul>
{% endblock %}
```
book_detail.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>{{ book.title }} by {{ book.author }}</h2>
<img src="{{ book.image.url }}" alt="{{ book.title }}">
<p>{{ book.description }}</p>
<p>Price: {{ book.price }}</p>
<a href="{% url 'add_to_cart' book.id %}">Add to Cart</a>
{% endblock %}
```
add_to_cart.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>Add to Cart - {{ book.title }}</h2>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Submit</button>
</form>
{% endblock %}
```
cart.html
```html
{% extends 'base.html' %}
{% block content %}
<h2>Cart</h2>
<ul>
{% for order in orders %}
<li>
{{ order.book.title }} x {{ order.quantity }} - {{ order.total_price }}
</li>
{% endfor %}
</ul>
{% endblock %}
```
以上仅是基于Django的图书二手交易系统的一个简单的实现示例,具体的实现会根据实际需求进行调整和完善。
阅读全文