Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Routing for FormView Class

I have django_test project and defapp application in it.

I want to access form.html which MyView makes,

I am still confused about routing.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I can access localhost/defapp/ and show Hello, World

However how can I access the MyView class and show form.html?

in django_test/urls.py

from django.contrib import admin
from django.urls import path
from django.conf.urls import include,url

urlpatterns = [
    path('defapp/', include('defapp.urls')),
    url(r'^s3direct/', include('s3direct.urls')),
    path('admin/', admin.site.urls),
]

in defapp/views.py

from django.urls import path
from . import views
 
urlpatterns = [
    path('', views.index, name='index')
]

in defapp/views.py

from django.shortcuts import render

# Create your views here.
from django.views.generic import FormView
from .forms import S3DirectUploadForm

def index(request):
    return HttpResponse("Hello, world.")
class MyView(FormView):
    template_name = 'form.html'
    form_class = S3DirectUploadForm

in defapp/template/form.html

<html>
<head>
    <meta charset="utf-8">
    <title>s3direct</title>
    {{ form.media }}
</head>
<body>
    <form action="" method="post">{% csrf_token %}
        {{ form.as_p }}
    </form>
</body>
</html>

>Solution :

If you’re looking to know how to wire up a class based view in the urlconf, please checkout these docs on class based views.

The most direct way to use generic views is to create them directly in your URLconf. If you’re only changing a few attributes on a class-based view, you can pass them into the as_view() method call itself:

from django.urls import path
from django.views.generic import TemplateView

urlpatterns = [
    path('about/', TemplateView.as_view(template_name="about.html")),
]

Additionally, the docs on as_view may prove helpful.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading