Nahidujjaman Hridoy
Nahidujjaman Hridoy

Reputation: 2227

How to save form data from base.html in django?

In my app, I have created a context_proccessors.py to show the form to base.html file.

I am able to show the form in the base.html file. But the problem I am facing is I have no idea how to save that form data from base.html since there is no view for the base.html. Below is my code:

models.py

class Posts(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='user_posts')
    post_pic = models.ImageField(upload_to='post_pic', verbose_name="Image")
    post_caption = models.TextField(max_length=264, verbose_name="Caption")
    created_date = models.DateTimeField(auto_now_add=True)
    edited_date = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f"{self.user.username}"

forms.py

from django import forms
from post_app.models import Posts


class PostForm(forms.ModelForm):
    class Meta:
        model = Posts
        exclude = ('user',)

context_proccessors.py

from post_app.forms import PostForm


def post_form(request):
    form = PostForm
    return {
        'post_form': form,
    }

base.html

<form method="POST" enctype="multipart/form-data">
                    {{ post_form|crispy }}
                    {% csrf_token %}
    <button type="submit" class="btn btn-primary">Post</button>

</form>

I want the form to be displayed on every page so that the user can submit data from anywhere

Upvotes: 0

Views: 816

Answers (2)

Nahidujjaman Hridoy
Nahidujjaman Hridoy

Reputation: 2227

By following the answer by N T I have implemented this. So, I had to make a URL pattern for the view and use that URL pattern in the action in the form of base.html.

view.py

@login_required
def postsaveview(request):
    form = PostForm()
    if request.method == 'POST':
        form = PostForm(request.POST, request.FILES)
        if form.is_valid():
            user_obj = form.save(commit=False)
            user_obj.user = request.user
            user_obj.slug = str(request.user) + str(uuid.uuid4())
            user_obj.save()
            return HttpResponseRedirect(reverse('profile_app:profile'))

urls.py

urlpatterns = [
    path('post-save/', views.postsaveview, name='post-save'),
]

base.html

<form action="{% url "post-save" %}" method="POST" enctype="multipart/form-data">
                    {{ post_form|crispy }}
                    {% csrf_token %}
    <button type="submit" class="btn btn-primary">Post</button>

</form>

Upvotes: 0

Nicol&#242; Teseo
Nicol&#242; Teseo

Reputation: 86

def PostView(request):
  form = PostForm()
  if request.method == 'GET':
     return render(request, 'base.html', {form:form})
  elif request.method == 'POST':
     form.save(request.data)

In the views.py of your app you can define this view, and the you have to provide it an url in the urls.py of the root directory. So evere time there is a request on that url, if the method is GET, the form will be rendered on base.html file, if the method is POST, the post will be saved.

Upvotes: 1

Related Questions