用Django写一个简单的问答系统
时间: 2023-10-22 17:05:16 浏览: 60
好的,下面是一个简单的用 Django 搭建问答系统的教程:
1. 确认 Django 环境已经搭建好,可以使用以下命令检查 Django 版本:
```
python -m django --version
```
2. 创建一个 Django 项目:
```
django-admin startproject myproject
```
3. 在 myproject 目录下创建一个 app:
```
cd myproject
python manage.py startapp qa
```
4. 在 qa 目录下创建一个名为 `urls.py` 的文件,用于配置问答系统的 URL:
```python
from django.urls import path
from . import views
urlpatterns = [
path('', views.home, name='home'),
path('q/<int:question_id>/', views.question_detail, name='question_detail'),
]
```
这里我们需要创建两个 URL,一个是首页(即 `/`),一个是问题详情页(即 `/q/1/` 等)。
5. 在 qa 目录下创建一个名为 `views.py` 的文件,用于处理 URL 请求:
```python
from django.shortcuts import render, get_object_or_404
from .models import Question
def home(request):
questions = Question.objects.all()
return render(request, 'qa/home.html', {'questions': questions})
def question_detail(request, question_id):
question = get_object_or_404(Question, pk=question_id)
return render(request, 'qa/question_detail.html', {'question': question})
```
这里我们需要创建两个视图函数,一个是首页的 `home`,一个是问题详情页的 `question_detail`。
6. 在 qa 目录下创建一个名为 `models.py` 的文件,用于定义数据模型:
```python
from django.db import models
class Question(models.Model):
title = models.CharField(max_length=200)
content = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
class Answer(models.Model):
content = models.TextField()
pub_date = models.DateTimeField(auto_now_add=True)
question = models.ForeignKey(Question, on_delete=models.CASCADE)
def __str__(self):
return self.content
```
这里我们需要创建两个数据模型,一个是问题(`Question`),一个是答案(`Answer`),并且使用外键关联问题和答案。
7. 在 myproject 目录下的 `settings.py` 中添加 `qa` 应用,配置模板路径和数据库:
```python
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'qa',
]
# 模板路径
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
# 数据库配置
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
```
8. 在 myproject 目录下创建一个名为 `urls.py` 的文件,用于配置全局 URL:
```python
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('qa.urls')),
]
```
这里我们需要将 `qa` 应用的 URL 包含进来。
9. 在 `qa` 应用的 `templates` 目录下创建两个 HTML 模板,分别是 `home.html` 和 `question_detail.html`,并写入以下代码:
`home.html`:
```html
{% extends 'base.html' %}
{% block content %}
<h1>Questions</h1>
<ul>
{% for question in questions %}
<li><a href="{% url 'question_detail' question.id %}">{{ question.title }}</a></li>
{% endfor %}
</ul>
{% endblock %}
```
`question_detail.html`:
```html
{% extends 'base.html' %}
{% block content %}
<h1>{{ question.title }}</h1>
<p>{{ question.content }}</p>
<h2>Answers</h2>
<ul>
{% for answer in question.answer_set.all %}
<li>{{ answer.content }}</li>
{% endfor %}
</ul>
{% endblock %}
```
这里我们使用了 Django 的模板语言,通过模板继承和代码块来实现页面的渲染。
10. 在 myproject 目录下创建一个名为 `base.html` 的文件,用于定义页面的基本结构:
```html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}QA System{% endblock %}</title>
</head>
<body>
{% block content %}{% endblock %}
</body>
</html>
```
这里我们定义了一个基本的 HTML 结构,包含了页面标题和内容块。
11. 运行 Django 项目:
```
python manage.py runserver
```
然后在浏览器中访问 `http://localhost:8000`,就可以看到问答系统的首页了。
以上就是一个简单的用 Django 搭建问答系统的教程。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)