2

I am writing an application with django. I know how to implement the django forms but what I want to do really is to have an html input input with type text or email or password and save the input to database or link it to the form.py

form.py

class PostForm(forms.ModelForm):

    publish = forms.DateField(widget= forms.SelectDateWidget)
    class Meta:
        model = Post
        fields = [
            "title",
            "content",
          ]

views.py

def create(request):
    form = PostForm(request.POST or None, request.FILES or None)
    if form.is_valid():
        instance = form.save(commit=False)
        instance.save()
        messages.success(request, "Post created")
        return HttpResponseRedirect(instance.get_absolute_url())
    context = {
        "form": form,
    }
    template = 'create.html'
    return render(request,template,context)

html

<form method="POST" action="" enctype="multipart/form-data">{% csrf_token %}
      {{ form.as_p }}
      <p>
      <input type="checkbox" id="test5" />
      <label for="test5">Red</label>
      </p>
      <input type="submit" name="submit" value="submit post">
  </form>
5
  • just access it request.POST.get('test5') I dont understand the question maybe Commented Aug 10, 2017 at 17:31
  • I am saying instead of "title" in form.py I would like to use ` <input type="text" name="title" value="title">` and the input would still submit into the database @JoranBeasley Commented Aug 10, 2017 at 17:42
  • @King you want to replace {{ form.as_p }} with individual html input tags? Commented Aug 10, 2017 at 17:47
  • exactly @BillF. Commented Aug 10, 2017 at 17:48
  • 1
    Well you can do exactly that if you really want. Did you try? What happened? But there's not really a good reason to do so. Commented Aug 10, 2017 at 17:58

2 Answers 2

1

Well just use this:

HTML

<input type='text' id='title'>

Insert following code under if form.is_valid()

views

title = request.POST.get('title')

post = Post(title=title)

post.save()


Sign up to request clarification or add additional context in comments.

2 Comments

would I have to include the post in my render? reference my view.py
Nope. Just write this code inside the if form.is_valid():
0

In the HTML form,
1. In the form tag, give the action as a URL
2. Then in urls.py, specify a view corresponding to that URL
3. Now, in your views.py write the view.
To get specific fields from your html form, you need to give the field an id in your template. Then in your view you can access particular fields by the ids.

Also, do specify the method=GET/POST in the HTML form, if you are using POST and would like to take only POST queries, then in your view just add if request.method=='POST'

Hope this helps!

3 Comments

can you break it down for me plese
My urls.py urlpatterns = [ url(r'^private/$', private),] My views.py def private(request): if request.method =="POST": name = request.POST['namev'] return render(request, "nonuser.html", {'name':name}) else: return render(request, "404.html")`
@King my html <form action="/posts/private/" method="POST">{% csrf_token %} <input type="text" id="namev" name="namev"><br> <input class="btn-warning" type="Submit" value="Submit" id="cli"> </form>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.