I need to create a simple input form without the use of a DB. Can anyone please assist?
Thnaks,

Dani AI

Generated

A single non-database input in Django is simplest built with a plain Form (not a ModelForm). As asked for a single-field form and requested code, below is a minimal, self-contained example showing what goes in forms.py, views.py, a template, and urls.py. For details on form widgets, validation and rendering see the Django forms docs.

# forms.py
from django import forms

class SimpleForm(forms.Form):
    name = forms.CharField(max_length=100, label='Name')
# views.py
from django.shortcuts import render, redirect
from .forms import SimpleForm

def simple_form_view(request):
    if request.method == 'POST':
        form = SimpleForm(request.POST)
        if form.is_valid():
            name = form.cleaned_data['name']
            # handle data without DB: write to file, send email, set session, etc.
            request.session['last_name'] = name
            return redirect('simple_thanks')
    else:
        form = SimpleForm()
    return render(request, 'form.html', {'form': form})
<!-- templates/form.html -->
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Submit</button>
</form>
# urls.py
from django.urls import path
from .views import simple_form_view

urlpatterns = [
    path('simple/', simple_form_view, name='simple_form'),
]

A few practical notes: include {% csrf_token %} (CSRF failures are common; see CSRF docs). After a successful POST use a redirect (Post/Redirect/Get) to avoid double submissions. If storing submissions to a file, guard against concurrent writes and never write sensitive data in plain text. models.py can remain empty for this pattern; switch to ModelForm later if you add a database.

Recommended Answers

All 2 Replies

I do not understand, post your Python code to see what you mean. (Paste-paint-push Code)

Sorry, my question is how do I create a simple form within Django with a single input field.
As all the examples are DB driven I cant seen to find anything that can set me in the right direction.

Im just starting Django so I pretty new to it. I suppose I just need to confirm what I would need in the model.py, views.py etc.
Any help would be really helpful....

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.