Home > Back-end >  Django - problem with writing to database
Django - problem with writing to database

Time:06-19

I have a problem, the urls form works but I can't see the records in url/admin, can I ask for help, thank you :D

SOF wants me to add more details otherwise it doesn't transfer, I don't know what more I can add, generally temapals and urls work.


class Note(models.Model):
    """..."""
    notes = models.CharField(max_length=100, unique=True)
    description = models.TextField()

    class Meta:
        verbose_name = "Note"
        verbose_name_plural = "Notes"

    def __str__(self):
        return self.notes

class NoteView(View):
    def get(self, request):
        if request.method == 'POST':
            textN = Note.objects.all().order_by('notes')
            form = NoteAddForm(request.POST)
            if form.is_valid():
                form.save()
                return redirect('Files/menu')
        else:
            textN = NoteAddForm()
        return render(request, 'Files/note.html', {'textN': textN})

class NoteAddForm(forms.ModelForm):
    """New note add form"""

    class Meta:
        model = Note
        fields = '__all__'

{% extends 'Files/base.html' %}

{% block title %}Notatnik{% endblock %}
<h2>Notatnik Dietetyka/ Zalecenia ręczne </h2>

{% block content %}

    <form action="/send/" method="post">
        {% csrf_token %}
        {{ textN }}
        <label>
            <input type="text" >
            <button><a href="{% url 'send' %}">Wyślij formularz</a></button>
        </label>


    </form>

    <button type="button" ><a href="{% url 'menu' %}">Powrót</a></button>
{% endblock %}

CodePudding user response:

Within your NoteView class in views.py file is where the issue is.

I see you have an if statement checking for if request.method == 'POST' within the class-based view get(). The get() is equivalent to if request.method == 'GET'. Therefore, what you might want to do is to override the post() on the class instead. For example:

class NoteView(View):
     template_name = 'Files/note.html'

     # Use the get method to pass the form to the template
     def get(self, request, *arg, **kwargs):
          textN = NoteAddForm()

          return render(request, self.template_name, {'textN': textN})

     # Use the post method to handle the form submission
     def post(self, request, *arg, **kwargs):
          # textN = Note.objects.all().order_by('notes') -> Not sure why you have this here...
          form = NoteAddForm(request.POST)

          if form.is_valid():
               form.save()

               # if the path is... i.e: path('success/', SucessView.as_view(), name='success')
               return redirect('success')  # Redirect upon submission 
          else:
               print(form.errors)  # To see the field(s) preventing the form from being submitted

          # Passing back the form to the template in the name 'textN'
          return render(request, self.template_name, {'textN': form})  

Ideally, that should fix the issue you're having.

Updates

On the form, what I'd suggest having is...

# Assuming that this view handles both the get and post request
<form method="POST">  # Therefore, removing the action attribute from the form
    {% csrf_token %}
    {{ textN }}

    # You need to set the type as "submit", this will create a submit button to submit the form
    <input type="submit"  value="Submit">
</form>

CodePudding user response:

ask solved - thank you Damoiskii

  • Related