Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you actually use a reusable django app in a project?

This question has been troubling me for some days now and I've tried asking in many places for advice, but it seems that nobody can answer it clearly or even provide a reference to an answer.

I've also tried searching for tutorials, but I just cannot find any type of tutorial that explains how you would use a reusable third-party django app (most tutorials explain how to write them, none explain how to use them).

Also, I've taken a look here:

How to re-use a reusable app in Django - it doesn't explain how to actually use it IN a project itself

and here:

How to bind multiple reusable Django apps together? - the answer by aquaplanet kind of makes sense, but I thought I would ask this question to solve the mental block I am facing in trying to understand this.


In order to best explain this, let me do so by example (note, it is not something I am actually building).

I am creating a project that acts like Reddit. I will have users, links and voting/points. Based on this crude example, I will want to reuse 3 (arbitrary) third-party apps: user, voting/points and links.

I decide to use each of them as any other python package (meaning that they will be treated as a package and none of their code should be touched) [would this method actually work? Or do you have to be able to edit third-party apps to build a project??)

With these apps now within my project, I will use a main app to handle all the template stuff (meaning everything I see on the frontend will be in a single app).

I will then either use that same main app for custom logic (in views.py) or I will break up that logic among different apps (but will still use a single frontend-only app).

From the 3 paragraphs above, is this structure applicable (or can it work) ?


Now lets say that this structure is applicable and I am using a single main app for the frontend and custom logic.

What would I write in models.py? How would I integrate things from the 3 reusable apps into the main models.py file?

How would I reference the reusable apps in views.py? Lets take the example of contrib.auth

With this built-in app, for logging out I would write:

from django.contrib.auth import logout
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect

@login_required
def user_logout(request):
    logout(request)
    return redirect('/home/')

Although the above code is simple, is that basically how things would be done with any reusable app?

My question is very long, but I think that this reusable app issue is something a lot of developers aren't quite clear about themselves and maybe this answer will help a lot of others who have heard about the promises of reusable apps, but fail to understand how to actually use them.

like image 901
Joe Avatar asked Feb 16 '14 13:02

Joe


2 Answers

TL;DR:

Nope & it depends...

Some (Very) Common Reusable Apps

  • django.contrib.admin
  • django.contrib.auth
  • django.contrib.staticfiles

... those are all reusable Django apps, that happen to be shipped with Django (most of them were not, at some point in time)

Ok, some other reusable apps that don't ship with Django:

  • django-rest-framework
  • django-registration
  • South

Those are all truly reusable apps, and nothing less. There are very many more apps like that.

How do they do it?

To me your question looks more like "how do I build reusable apps", then "how to use them". Actually using them is very different from app to app, because they do very different things. There is only one rule: RTFM No way around that either.

Often, they rely on one or more of the following:

  • additional value(s) in settings.py
  • addition (usually one include statement) to urls.py
  • subclassing and/or mixins for Models, Forms, Fields, Views etc.
  • template tags and/or filters
  • management commands
  • ...

Those are all powerful ways though which your app can provide functionality to other apps. There is no recipe (AFAIK) to make a reusable app, because there are so many different scenarios to consider. It all depends on what exactly your app should do.

Reusable apps provide functionalities

I'd argue that it's important to not think of reusable apps as "working together" with other app, but instead recognize that that they "provide functionality." The details of the functionality provided should dictate the way the target developer is supposed to use your library.

Not everything should be reusable

Obviously enough, even though many apps can "in principle" be reusable, it often makes little sense to do so, because it is way faster to clump things together (and make them just "work together").

like image 109
frnhr Avatar answered Oct 10 '22 18:10

frnhr


I'm not sure why you think you need a main app for the "frontend" stuff. The point of a reusable app is that it takes care of everything, you just add (usually) a single URL to include the urls.py of the app, plus your own templates and styling as required.

And you certainly don't need to wrap the app's views in your own views, unless you specifically want to override some functionality.

I don't understand at all your question about models. There's no such thing as a "main" models file, and using a reusable app's models is just the same as using models from any of your own apps.

Normally you would not edit a third-party app, that would make it very hard to integrate updates. Just install the app in your virtualenv (you are using virtualenv, of course!) with pip, which will put it in the lib directory, and you can reference it just like any other app. Make sure you add it to INSTALLED_APPS.

like image 42
Daniel Roseman Avatar answered Oct 10 '22 19:10

Daniel Roseman