简体   繁体   中英

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:

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

In 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. 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)

I am newbie in Django :) want your help

You need to allow the URL to accept parameters to allow you specify which event category you want to view:

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

Do do this, you use a named URL pattern in your url scheme:

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.

It's better that you follow the Django tutorial first, this is all covered in there. 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.

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 .

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

The Django Admin will allow you to edit all model fields if this is what you are after. Instructions on setting it up can be found in the Django documentation.

However I think what you are asking is how to enable CRUD editing through the web to users who are not admin level users. In that case you have many options. One of those options is to use a pre-built framework for Django like piston . 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. You can pass parameters to your custom functions within the httprequest, for example as POST data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM