3

My project consists of two versions of the same thing. For example version 1 is Freshman and version 2 is Sophomore. To avoid redundancy I am using the same templates and views as the implementation in both the version is not very different. The only difference in this site would be in the urls. Such that

localhost:8000/freshman/computer-science
localhost:8000/sophomore/computer-science

The user of my site can switch to any version of the site by just clicking on a button namely Freshman or Sophomore. I am confused about HOW TO implement such a method through which I would not have repeat myself and can achieve the functionality in a pythonic way.

Zain Khan
  • 3,753
  • 3
  • 31
  • 54
  • Do you need this: [Python + Django page redirect](http://stackoverflow.com/questions/523356/python-django-page-redirect) ? – gecco Dec 16 '11 at 21:02
  • 1
    @gecco Not at all. This is very different to what I have asked. Though thank you – Zain Khan Dec 16 '11 at 21:12
  • I think it might be helpful if you could explain what the differences would be, other than the url - will they access different databases, or are there different model objects for each group, stored in the same database, or what? – Marcin Dec 16 '11 at 21:15
  • @Marcin: 90% of the models are the same except for some fields but yes the content for both these versions would be quite different. I repeat data would be different **NOT** the db – Zain Khan Dec 16 '11 at 21:29
  • @Zulaikha: I.e. it's different data stored in the same database, or do you mean different data, different database, same database schema? – Marcin Dec 16 '11 at 21:46

3 Answers3

2

I would suggest looking into using one url pattern for both, but pass unique parameters into each. You achieve this using the django.con.urls.defaults.include function. In this case, the first include sets the kwarg current_app to freshman, and the second to sophomore.

from django.conf.urls.defaults import *

site_patterns = patterns('',
    # put your urls here...
)

urlpatterns = patterns('',
    url(r'^freshman/',
        include(site_patterns, namespace='freshman', app_name='freshman'), {
            'current_app': 'freshman',
        }),
    url(r'^sophomore',
        include(site_patterns, namespace='sophomore', app_name='sophomore'), {
            'current_app': 'sophomore',
        }),
)

Then, like usual you can pop the kwarg in your view, and trigger the unique behavior off that value:

def my_view(request, *args, **kwargs):
    current_app = kwargs.pop('current_app')

Alternately, you could create a decorator to automate this and any other boilerplate that needs to be done on views following this format.

Daniel Naab
  • 22,690
  • 8
  • 54
  • 55
  • Can I access the kwargs in template? As I said that I have one template for both, I would have to use an if else condition to cater both cases – Zain Khan Dec 16 '11 at 21:19
  • You can access anything if you explicitly pass it into the template's context. I would suggest creating a decorator for these views to handle things like that for you automatically, and/or use a combination of middleware and context processors to inject the information you need into the request and request context. – Daniel Naab Dec 16 '11 at 22:18
0

There are a many method's through which this functionality can be achieved using the django framework. One way that I can suggest right now is by using django session

The complete documentation of sessions is available HERE

You can keep a variable in sessions, let call it type. When the user clicks on Freshman the type would be 'f' or 's' in the later case. So

request.session[ 'type' ] = 'f' #when Freshman is clicked
request.session[ 'type' ] = 's' #when Freshman is clicked

To get which type is being used for the site, simply

request.session.get( 'type', '' ) #this would return 'f' || 's' || ''

'' would be in the case when a user has not clicked on any of the two options.

Though this is a very simple and applicable way but to be honest I am not a big fan of using session, therefore, I would be anxious to see more methods to be listed.

Zain Khan
  • 3,753
  • 3
  • 31
  • 54
0

If what you mean is that the code and database schema are the same, but there is no shared data, then I would suggest that you deploy two complete, separate instances of your application to the one server (or more, depending), and have your webserver route /sophomore requests to one app, and /freshman to the other. Have a separate development/testing setup, and use version control to push code changes to both installations.

With the above setup, you can either have two separate databases, or you can share one database, and put things in your config to tell your application how to disambiguate what it is reading and writing. This second approach is trickier.

A third approach is to do what you would do for the second approach, except that you deploy one application instance, and instead of examining the config to determine if the app is running in sophomore or freshman mode, all of your urls have a parameter as their first element, and in each view you examine the value of that parameter (or, django might have a hook for pre-processing each request in which you could do the necessary).

Marcin
  • 48,559
  • 18
  • 128
  • 201