Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

python - Get HTML form in Django fuction

I know there are explanations out there regarding this, but I didn't understand how to implement it in my code, so I am asking again.

I get the following error

ViewDoesNotExist at /answer_question/
'False' is not a callable or a dot-notation path

When I click the "Post your answer" button in my post.html form. It is like a Stack Overflow website.

enter image description here

I have an URL called answer_question/ in urls.py

path('answer_question/', views.answer_question, name="answer_question"),

Then I made a function in views.py

def answer_question(request):

    return render(request, 'store/homepage.html')
    def post(self, request):
        form = HomeForm(request.POST)
        if form.is_valid():
            text = form.cleaned_data["post"]
            print("TEXT = " + str(text))

This has to make a new answer in the database.
models.py

class Answer(models.Model):
    title = models.CharField(max_length=200, null=True)
    context = models.TextField(max_length=1702, blank=True, validators=[MaxLengthValidator(1702)])
    date = models.DateField(("Date"), default=date.today)
    author = models.ForeignKey(Customer, on_delete=models.CASCADE, null=True)
    post = models.ForeignKey(Post, on_delete=models.CASCADE, null=True)

    def __str__(self):
        return self.title

As for the syntax I looked at: https://www.youtube.com/watch?v=wzZiONbtwiA and https://docs.djangoproject.com/en/3.1/topics/forms/


<form action="/answer_question/" method="post">
<div class="blogpost"><br><br>
  <textarea name="answer-title-input" cols="20" rows="1" class="answer-title" maxlength="20" placeholder="Write a title here..." id="id_context"></textarea>
  
  <br><br><hr><br>

  <div class="blogpost-content">
    <textarea name="answer-context-input" cols="80" rows="10" class="answer-input" maxlength="300" placeholder="Write an answer here..." id="id_context"></textarea>
  <br>
  <br>
  <button type="submit" class="submit-answer-button">Post your answer</button>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I ended up using this

<form method="post" action="answer_question/">
  {% csrf_token %}

    <input type="text" name="value1" placeholder="Value 2">
      <input type="text" name="value2" placeholder="Value 2">
       <input type="submit" value="Click here!">

</form>

And in my code

<div class="blogpost"><br><br>
  <form method="post" action="answer_question/">
  {% csrf_token %}
  <textarea name="answer-title-input" cols="20" rows="1" class="answer-title" maxlength="20" placeholder="Write a title here..." id="id_context"></textarea>
  
  <br><br><hr><br>

  <div class="blogpost-content">
    <textarea name="answer-context-input" cols="80" rows="10" class="answer-input" maxlength="300" placeholder="Write an answer here..." id="id_context"></textarea>
  <br>
  <br>
  <button type="submit" class="submit-answer-button">Post your answer</button>
  <br>
  <br>
</div></div>
<br><br><br><br><br><br>
</form>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...