简体   繁体   English

如何在Django-Python中将值从views.py传递到已定义的模板

[英]How to pass values to defined template from views.py in Django-python

I have a model like this: 我有一个这样的模型:

class EventTypeCategory(models.Model):
    name = models.CharField(max_length=50, verbose_name="Name")
    user = models.ForeignKey(User, verbose_name="User")
    Message_slug = models.SlugField(blank=True, verbose_name="Message")

       def __unicode__(self):
           return self.name

In urls.py: 在urls.py中:

url(r'^categ/$',
    'eventcateg_detail', name='eventcateg_detail'),

In views.py: 在views.py中:

def eventcateg_detail(request,event_categ_id=None, event_categ_slug=None):

I want to add/edit/delete(CRUD) above defined value ie name and Message_slug by template level. 我想按模板级别在定义的值(即名称和Message_slug)之上添加/编辑/删除(CRUD)。 I am not getting any hint how to relate url.py with views.py and what should be definition of eventcateg_detail function.How this function will pass values to template (template name will be categ.html) 我没有任何提示如何将url.py与views.py相关联以及eventcateg_detail函数的定义是什么。此函数如何将值传递给模板(模板名称为categ.html)

I am newbie in Django :) want your help 我是Django的新手:)需要您的帮助

You need to allow the URL to accept parameters to allow you specify which event category you want to view: 您需要允许URL接受参数,以允许您指定要查看的事件类别:

/categ/outdoor-events/
/categ/catered-events/ 
...

Do do this, you use a named URL pattern in your url scheme: 为此,您在url方案中使用命名URL模式

url(r'^categ/(?P<slug>[-\w]+)/$','eventcateg_detail', name='eventcateg_detail'),

and in your view: 并且在您看来:

from django.shortcuts import get_object_or_404, render
def eventcateg_detail(request,slug):
    return render(request, "categ.html", {
        'obj' : get_object_or_404(EventCateg, Message_slug =slug) # You should change Message_slug to just slug
    })

and in your template: 并在您的模板中:

<h1>{{ obj.name }}</h1>

So when a user enters a URL like we have outlined above, it gets matched to our URL pattern and the slug part of the url ( catered-events ) gets passed as a parameter to our view. 因此,当用户输入如上所述的URL时,它将与我们的URL模式匹配,并且url的子弹部分( catered-events )将作为参数传递给我们的视图。

It's better that you follow the Django tutorial first, this is all covered in there. 最好先阅读Django教程,此处已介绍了所有内容。 See for example part 3 of the tutorial for more information on how to relate urls.py with views.py and part 4 discusses passing variables to the template. 例如,请参阅本教程的第3部分 ,以获取有关如何将urls.py与views.py相关联的更多信息, 第4部分讨论了如何将变量传递给模板。

I believe that a view function is only passed an httprequest when it is called by the Django framework, the other two parameters of the function will only be useful if you call the function yourself but will not be useful through the web . 相信 ,只有在Django框架调用视图函数时,视图函数才会传递httprequest,该函数的其他两个参数仅在您自己调用该函数时才有用, 而在Web上则无用。

As pointed out in the comments I was mistaken in my belief, extra parameters can be passed as dynamic urls (ie urls designated like this url(r'^polls/(?P<poll_id>\\d+)/$', 'polls.views.detail') ,. See this link and the answer by @pastylegs 正如评论中指出的那样,我误认为我的信念,可以将其他参数作为动态url传递(即,像这样指定的url(r'^polls/(?P<poll_id>\\d+)/$', 'polls.views.detail')请参阅此链接和@pastylegs的答案

The Django Admin will allow you to edit all model fields if this is what you are after. 如果您要这样做,那么Django Admin将允许您编辑所有模型字段。 Instructions on setting it up can be found in the Django documentation. 设置说明可在Django文档中找到。

However I think what you are asking is how to enable CRUD editing through the web to users who are not admin level users. 但是,我认为您要问的是如何通过Web对非管理员级别的用户启用CRUD编辑。 In that case you have many options. 在这种情况下,您有很多选择。 One of those options is to use a pre-built framework for Django like piston . 这些选择之一是为Django使用类似于活塞的预构建框架。 Another way would be to use generic views 另一种方法是使用通用视图

The other option is to build views yourself enabling operations on your model. 另一个选择是自己构建视图,从而在模型上启用操作。 In that case all of Django is available to you. 在这种情况下,您可以使用所有Django。 You can pass parameters to your custom functions within the httprequest, for example as POST data. 您可以在HttpRequest的参数传递给您的自定义功能,例如POST数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 django-python在views.py中创建多维数组 - django-python create Multidimensional Array in views.py 如何通过Django中的views.py将models.py中的多个值传递给HTML - How to pass multiple values from models.py to HTML via views.py in Django 在Django中,如何访问views.py中的模板值? - In Django, how do I access template values in the views.py? Django / Python:将值从模板拉入views.py - Django/Python: Pulling value from template into views.py 如何从views.py中的Django模板获取变量的值? - How to get value in variable from Django template in views.py? 使用Django中views.py中的值运行python脚本 - running a python script with values from views.py in Django 如何从views.py获取值并在html模板中使用它? - how to get values from views.py and use it in html template? 如何将变量从 Django views.py 传递到另一个 Python 文件? - how to pass a variable from the Django views.py to another Python file? 如何在不使用 forms 的情况下使用 for 循环将数据从 django 模板中创建的多个复选框传递到 views.py - how to pass data from multiple checkboxes created in django template using for loop to views.py without using forms 如何使用 javascript 在 django 模板中设置变量并将其传递给 views.py? - How to set variable inside django template with javascript and pass it to views.py?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM