I am trying to create a webpage where you can upload questions to the Questions
database. I was wondering is there any easy way to do this in Django? Can I upload it so it will be accessible from the Django admin? Here is what I have.
我正在尝试创建一个网页,您可以将问题上传到问题数据库。我想知道Django有没有简单的方法呢?我可以上传它,以便可以从Django管理员访问它吗?这就是我所拥有的。
#Models
class Question(models.Model):
question = models.CharField(max_length=400)
answer = models.CharField(max_length=400)
def __unicode__(self):
return self.question + "?"
class QuestionForm(ModelForm):
class Meta:
model = Question
fields = ['question', 'answer']
#Question Template
<div class="container" align="center">
<div class="hero-unit3" align="center">
<h3>
Feel free to post some questions, and a DarKnight representative will answer them for you.
</h3>
</div>
</div>
</div>
<div class="row">
<div class="span6">
<h4>
<form action="<!-- NO IDEA WHAT TO DO -->" method="post">
<input type="text" name="question" />
</div>
</div>
</div>
#views.py
class question(generic.ListView):
template_name = 'users/question.html'
context_object_name = 'Question_list'
def get_queryset(self):
return Question.objects.order_by('question')
2 个解决方案
#1
1
The easiest way to achieve what you need is to use CreateView.
实现所需的最简单方法是使用CreateView。
In views.py:
from django.views.generic.edit import CreateView
from yourapp.models import Question
class QuestionCreate(CreateView):
model = Question
fields = ['question', 'answer']
Create a new template name question_form.html
:
创建一个新的模板名称question_form.html:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create" />
</form>
Hope it helps!
希望能帮助到你!
#2
0
To make a model available to django admin you have to register the model to admin by
要使模型可用于django admin,您必须将模型注册到admin
from django.contrib import admin
class Question(models.Model):
...
admin.site.register(Question)
Also for doing this from custom template you can use a model form
此外,为了从自定义模板执行此操作,您可以使用模型表单
The form can be displayed in the template as a table or as a paragraph.
表单可以作为表格或段落显示在模板中。
Suppose you render the form to the template as f
, use it in template as follows
假设您将表单作为f呈现给模板,请在模板中使用它,如下所示
<form action='..' method='post'>
{{ f.as_t }} //or f.as_p for paragraph
</form>
#1
1
The easiest way to achieve what you need is to use CreateView.
实现所需的最简单方法是使用CreateView。
In views.py:
from django.views.generic.edit import CreateView
from yourapp.models import Question
class QuestionCreate(CreateView):
model = Question
fields = ['question', 'answer']
Create a new template name question_form.html
:
创建一个新的模板名称question_form.html:
<form action="" method="post">{% csrf_token %}
{{ form.as_p }}
<input type="submit" value="Create" />
</form>
Hope it helps!
希望能帮助到你!
#2
0
To make a model available to django admin you have to register the model to admin by
要使模型可用于django admin,您必须将模型注册到admin
from django.contrib import admin
class Question(models.Model):
...
admin.site.register(Question)
Also for doing this from custom template you can use a model form
此外,为了从自定义模板执行此操作,您可以使用模型表单
The form can be displayed in the template as a table or as a paragraph.
表单可以作为表格或段落显示在模板中。
Suppose you render the form to the template as f
, use it in template as follows
假设您将表单作为f呈现给模板,请在模板中使用它,如下所示
<form action='..' method='post'>
{{ f.as_t }} //or f.as_p for paragraph
</form>