1

Questions質問をデータベースにアップロードできる Web ページを作成しようとしています。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')
4

2 に答える 2

1

必要なものを実現する最も簡単な方法は、CreateViewを使用することです。

views.py で:

from django.views.generic.edit import CreateView
from yourapp.models import Question

class QuestionCreate(CreateView):
    model = Question
    fields = ['question', 'answer']

新しいテンプレート名を作成しますquestion_form.html:

<form action="" method="post">{% csrf_token %}
    {{ form.as_p }}
    <input type="submit" value="Create" />
</form>

それが役に立てば幸い!

于 2013-07-26T19:42:00.840 に答える