重用 django 可重用应用程序

发布于 2024-10-02 11:52:27 字数 633 浏览 2 评论 0原文

我想知道这是否可以实现(作为应用程序/中间件):

我安装 django-registration 应用程序。然后,我创建基于站点的应用程序来制作一些通用页面视图。我想在首页上放置一个登录表单和一个注册表单。所以我进去修改 /register/login.html 和 register/register.html 模板以适合我的首页设计(html 内容)。然后我转到我的主页index.html文件,然后转到我的html中我想要这些块(登录和注册)的位置,然后添加 {% load "register/login.html" %} 和{%加载“register/register.html”%}。现在,当 urlconf 调用我的索引视图时,模板将到达 LOAD 触发器并调用 LOGIN 视图,以便将其所有 form.elements 传递给它,并且也会为其元素调用 REGISTER 视图。然后,这些完成的(渲染的)视图被传递到我的 index.html 并插入到我放置 LOAD 语句的位置。

以上目前可以做到吗?我的目标是获取各种可用的应用程序并将它们插入到我的项目中,而不触及任何代码(我想确保我可以稍后升级各个应用程序,并且不会破坏我的项目中的任何内容,因为我添加了自定义内容......) 。

如果上述目前可行,有人可以提供一些文档/教程/操作方法,以了解重用其他人的应用程序的最佳实践吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-10-09 11:52:27

当然还有 {% include %} 标签,它允许您将模板直接包含在另一个模板中。它还获取包含的模板获取的所有内容,因此,如果您使用 RequestContext,则意味着它可以访问请求变量中的所有内容。

但是,您似乎是说您希望以某种方式实际调用注册视图和登录视图并将结果嵌入到您的页面中。理论上,这可以通过编写一个使用 http GET 调用 URL 的自定义标记,然后输出请求中生成的 HTML 来实现。

我不会推荐这个。相反,对于首页,请继续创建两个指向 django-registration 应用程序中相应 URL 的表单。

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.

℡寂寞咖啡 2024-10-09 11:52:27

简单地修改我重新使用的应用程序中的视图以包含一个额外的参数来查看它是否被用作 SUBVIEW (因此不返回 render_to_response() ),并检查 request.POST 中的“FORMNAME”怎么样?数据。然后,如果 SUBVIEW(我正在重复使用的应用程序的视图)在 request.POST 中找到其“FORMNAME”,它将处理该表单。如果没有提供 request.POST 数据,它将返回包含所有表单元素的字典,而不是 render_to_response()。然后,我可以在首页视图中调用该函数,并将返回的值字典以及任何其他组件传递到我的模板。提交时,将调用该函数,如果它在 request.POST 数据中找到“表单名称”(此“表单名称”可以位于隐藏字段中),它将处理该表单,否则将返回表单元素的字典,并且将在我的视图中调用下一个函数,这可能与 django.contrib.django-registration.register() 视图相关,这将产生最终的可重用性

! form.errors!!

我的观点:

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})

或者我可以分叉每个应用程序并修改它......这违背了将应用程序作为独立维护的包重新使用的目的,而更像是复杂的代码粘贴。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文