5

I am kind of new to Django (been writing Python for a while now) and I was wondering if it is at all possible to use the Views and Forms framework in Django without having the need for a database or the use of Models.

The content is dynamic, and gets populated into a dictionary each time the user launches the website. So I would like to leverage the Django Views and Forms but passing through the details from the dictionary and not from a Model Class (Database)

I hope I have explained myself correctly.

So to add onto this then.

If you had the following for a Models based class:

class TestClass(models.Models):  
    var1 = models.CharField(....)

class DetailView(generic.DetailedView):  
    model = TestClass

How could I use the DetailView class (Or any other django based View framework with a class based dictionary like this:

class TestObj(object):  
   def __init__(self):
       self.var1 = []

testdict = defaultdict(TestObj)
testdict['test'].var1 = ['blah']

Do I just use the dictionary in the model field under the DetailView class ?

please excuse me if I have anything typed incorrectly.

Thanks

0

1 Answer 1

5

It is totally possible.

However, if you decide not to have a database you will lose a chance to use many contrib features (like accounts, backend). From the "empty" project (made with django-admin startproject) you will need to remove most of INSTALLED_APPS, MIDDLEWARE, context_processors in TEMPLATE, set DATABASES to an empty dict() (see the comment from @spectras), maybe some other settings tinkering on top.

Upd: (Since you modified your question significantly, here are more details)

Using django.db.models doesn't make much sense without a database (pay attention on db in the path to models package). Some generic views (like DetailView) are made to spare your time when working with models, so using them will not make much sense neither.

If you don't need to save any of TestClass instances and their lifetime is limited to generating a response to a single user request, you should be fine without using django models at all. Using TemplateView will be sufficient.

    class TestClass(object):
        def __init__(foo, bar):
            self.foo, self.bar = foo, bar

    class MyView(TemplateView):
        template_name = 'my_template.html'

        def get_context_data(self, **kwargs):
            context = super().get_context_data(**kwargs)
            context['my_object'] = TestClass(foo="hello", bar="world")
            return context

In my_template.html:

    {{ my_object.foo }} {{ my_object.bar }}
Sign up to request clarification or add additional context in comments.

1 Comment

I remember setting DATABASES to {} raised an error at some point. You had to provide a dummy default key, like this: {'default': {}}. Don't know if it still applies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.