开发者

Reusing django re-usable apps

I was wondering if this would be possible to implement (as an app/middleware):

I install the django-registration app. I then create my site-base app for making some generic page views.开发者_运维知识库 I want to put a login form and a registration form on a the front page. So I go in and I modify the /register/login.html and the register/register.html templates to fit my front page design (html stuff). I then go to my main page index.html file and I go to the spot in my html where I want those blocks (login & register) to go, and I add {% load "register/login.html" %} and a {% load "register/register.html" %}. Now, when the urlconf calls my index's view, the template will reach the LOAD trigger and will call the LOGIN view so that all of its form.elements are passed to it, and the REGISTER view is called for its elements too. Then, those completed (rendered) views are passed to my index.html and plugged into the spot where I put the LOAD statements.

Can the above be done currently? My goal is to take the various apps available and plug them into my project without touching any of their code (I want to ensure that I can upgrade the individual apps later and not break anything in my project because I added custom stuff...).

If the above is possible currently, could someone please provide some documentation/tutorials/howtos for best practices in re-using other peoples apps?


There's certainly the {% include %} tag, which allows you to include templates directly inside of another template. It also gets everything that the enclosed template gets, so if you are using the RequestContext that means it has access to everything in the request variable.

However, it seems you're saying that you want to somehow actually call the register view and login views and embed the result into your page. This could in theory be possible by writing a custom tag that calls the URL using an http GET and then outputting the resulting HTML from the request.

I wouldn't recommend this. Instead, for the front page, go ahead and create two forms that point to the appropriate URLs in the django-registration application.


What about simply modifying the views in the app I am re-using to include an extra argument to see if it is being used as a SUBVIEW (and therefore not return render_to_response() ), and check for "FORMNAME" in the request.POST data. Then, if the SUBVIEW (view of app I am re-using) finds its "FORMNAME" in the request.POST it would process the form. If no request.POST data supplied, it would return a dictionary with all the form elements rather than the render_to_response(). I can then call that function in my view for the front page and pass the returned dictionary of values to my template along with any other components. On submit, the function would be called, and if it finds the "Name of form" in the request.POST data (this "Name of form" can be in a hidden field, it will process that form, otherwise it will return the dictionary of form elements, and the next function will be called in my view, which may be related to the django.contrib.django-registration.register() view. This would produce ULTIMATE RE-USABILITY!

This way I also get access to the form.errors!!

My view:

def index(request):
   login = django.contrib.register.login(request, ... , Subview=True)
   register = django.contrib.register.register(request, ... , Subview=True)

   return render_to_response('index.html', {'login_form': login, 'register_form': register})

Alternatively I could fork each app and modify it... which defeats the purpose of re-using the app as an independently maintained package, and rather is treated more like a complex code paste.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜