CRUD template for urls.py, passing an object from a URI for viewing

Is it possible to view the value of an object and pass it into a general view? Can general views need to be transferred to views.py to support object lookups?

urls.py

urlpatterns = patterns('myapp.views',
        url(r'^mymodel/create/$', view='mymodel_create',  name='mymodel_create'),
)

urlpatterns += patterns('django.views.generic',
        url(r'^(?P<model>\w+)/create/$','create_update.create_object', name='object_create'),
        url(r'^(?P<model>\w+)/(?P<id>\d+)/update/$', 'create_update.update_object', name='object_update' ),
        url(r'^(?P<model>\w+)/(?P<id>\d+)/delete/$', 'create_update.delete_object', name='object_delete'),        url(r'^(?P<model>\w+)/(?P<object_id>\d+)/$', 'list_detail.object_detail',  name='object_detail'),
        url(r'^(?P<model>\w+)/$','list_detail.object_list', name='object_list'),'
)

Example URL:

http://localhost/myapp/thing/create
http://localhost/myapp/thing/1
http://localhost/myapp/thing/1/update
http://localhost/myapp/thing/1/delete

I would like to use (? P \ w +) to find the corresponding Thing model, but when this code is executed, the following error occurs: object_list () received an unexpected keyword argument to 'model'

+3
source share
2 answers

, , . Django . , . ( , ). URLconf, "model" "object", :

urls.py:

url(r'^(?P<model>\w+)/(?P<object_id>\d+)/$', 'my_app.views.my_object_detail',  name='object_detail'),

my_app/views.py

from django.views.generic.list_detail import object_detail
from django.db.models.loading import get_model
from django.http import Http404

def get_model_or_404(app, model):
    model = get_model(app, model)
    if model is None:
        raise Http404
    return model

def my_object_detail(request, model, object_id):
    model_class = get_model_or_404('my_app', model)
    queryset = model_class.objects.all()
    return object_detail(request, queryset, object_id=object_id)

, , , , , , , , ..

+4

, , . django.views.generic.list_detail, , object_list, object_detail "object", "queryset". " " - - ( object_detail, object_id, slug slug_field). , , , - , .

+1

Source: https://habr.com/ru/post/1716443/


All Articles