Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Import a class into the urls.py in Django

In the last few weeks I've switched from developing an application that processes a simple XML file, then writes its contents to an Oracle DB (cx_Oracle) and outputs in HTML, to using a Django framework. The Django framework switch wasn't necessary, but since I have an opportunity to develop something by using Django, I thought why not as it is a new area for me and can't damage my CV.

Anyway, I'm having issues with knowing what to write in my urls.py file when importing a Class from the views.py file. Here are the current contents:

urls.py

from myproj.views import Pymat_program

pymatProgram = Pymat_program()

urlpatterns = (
    url(r'^pymat/$', pymatProgram.abc),
)

views.py

class Pymat_program:
    def abc(self, request):
        test = "<html><body>Random text.</body></html>"

        return HttpResponse(test)

I've tried various permutations of using request, not using request, and also how the class is called in the url tuple, all to no avail. When I use a definition outside of the class (i.e. not in any class), then this is correctly displayed in HTML.

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You don't want to wrap your program in a class. (In general, in Python you should treat modules like, say, Java might treat classes with static members only.)

There are two approaches, really:

Function-based views

urls.py

from myproj.views import abc_view

urlpatterns = (
    url(r'^pymat/$', abc_view),
)

views.py

def abc_view(request):
    test = "<html><body>Random text.</body></html>"
    return HttpResponse(test)

Class-based views

urls.py

from myproj.views import AbcView

urlpatterns = (
    url(r'^pymat/$', AbcView.as_view()),
)

views.py

from django.views.generic import View

class AbcView(View):
    def get(self, request, *args, **kwargs):
        test = "<html><body>Random text.</body></html>"
        return HttpResponse(test)
over 4 years ago · Santiago Trujillo Report

0

Since you are using class based views in Django, so you have to call this class in URL like:

from myproj.views import Pymat_program

pymatProgram = Pymat_program()

urlpatterns = (
    url(r'^pymat/$', pymatProgram.as_view()),
)

and then use get or post methods name in class like:

class Pymat_program:
    model = ModelName

    def post(self, request, *args, **kwargs):
        ....

2nd way is to use function based views:

urls.py

from django.conf.urls import url

from . import views

urlpatterns = [
    url(r'^articles/2003/$', views.view_name),
]

views.py

from django.http import HttpResponse
import datetime

def view_name(request):
    now = datetime.datetime.now()
    html = "<html><body>It is now %s.</body></html>" % now
    return HttpResponse(html)

for more details look into class based views docs

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!