I am writing a basic Events app which contains of two modules(apps) so far : users and events.
I am using Django 2.1 with Python 3.6 on Ubuntu 16.04
So far, I've been able to handle users, but on events, I can't use Update, Detail and Delete generic views. All of them return 404.
My views.py:
class EventListView(ListView):
model = EventModel
template_name = 'event_list.html'
queryset = EventModel.objects.order_by('start_date_time')
class EventUpdateView(UpdateView):
model = EventModel
fields = ['event_type','start_date_time'
]
template_name = 'event_update.html'
class EventDeleteView(DeleteView):
model = EventModel
template_name = 'event_delete.html'
success_url = reverse_lazy('event_list')
class EventDetailView(DetailView):
model = EventModel
template_name = 'event_detail.html'
My urls.py (in project folder):
urlpatterns = [
path('', include('pages.urls')),
path('admin/', admin.site.urls),
path('users/', include('users.urls')),
path('users/', include('django.contrib.auth.urls')),
path('events/', include('events.urls')),
]
My urls.py (in events app):
urlpatterns = [
path('', views.EventListView.as_view(), name='event_list'),
path('<int:id>', views.EventDetailView.as_view(), name='event_detail'),
path('<int:id>/edit/', views.EventUpdateView.as_view(), name='event_update'),
path('<int:id>/delete/', views.EventDeleteView.as_view(), name='event_delete'),
]
What am I doing wrong? I've been searching the whole day and still have no idea how this might be wrong.
Note that the first line works (EventListView) but the other lines don't. By the way, I am using the book Django for Beginners. Most of the code here is identical to the code in the book.
Update
I don't use namespace in this application, the rest of urls.py is only some basic imports :
from django.urls import path
from . import views
The urls.py for the Project is like above, except it has include and admin as well.
The examples of URLs giving 404 error:
http://127.0.0.1:8000/events/1/
http://127.0.0.1:8000/events/1/edit/
PS I thought edit and delete give me 404, but actually the error is :
ImproperlyConfigured at /events/1/edit/
EventUpdateView is missing a QuerySet. Define EventUpdateView.model, EventUpdateView.queryset, or override EventUpdateView.get_queryset().)
In short: you defined a models (with s) attribute, but it should be model (without s).
Well the error actually already explains the problem:
ImproperlyConfigured at /events/1/edit/ EventUpdateView is missing a QuerySet.
Define EventUpdateView.model, EventUpdateView.queryset,
or override EventUpdateView.get_queryset().)
In your EventUpdateView you did not specify a model attribute, you wrote models, and for Django that is an entirely different attribute. So you should rename it to:
class EventListView(ListView):
model = EventModel
template_name = 'event_list.html'
queryset = EventModel.objects.order_by('start_date_time')
class EventUpdateView(UpdateView):
model = EventModel
fields = ['event_type','start_date_time'
]
template_name = 'event_update.html'
class EventDeleteView(DeleteView):
model = EventModel
template_name = 'event_delete.html'
success_url = reverse_lazy('event_list')
class EventDetailView(DetailView):
model = EventModel
template_name = 'event_detail.html'
For the EventListView, that did not matter, since you also defined a queryset attribute, and so Django took that one, but I would update it anyway.
Furthermore in the urls.py, you need to specify a pk parameter by default:
urlpatterns = [
path('', views.EventListView.as_view(), name='event_list'),
path('<int:pk>', views.EventDetailView.as_view(), name='event_detail'),
path('<int:pk>/edit/', views.EventUpdateView.as_view(), name='event_update'),
path('<int:pk>/delete/', views.EventDeleteView.as_view(), name='event_delete'),
]
Finally in the template you wrote something like:
{% url 'event_update' event.id %}
But apparently there was no event identifier, as a result the event.id is the string_if_invalid (by default the empty string), which is not an integer (well at least not if you did not specify that), and hence it can not find a relevant URL. After some discussion, it turned out that the correct identifier was object, so the correct url is something like:
{% url 'event_update' pk=object.id %}
The same of course should happen with other {% url ... %} calls.
Related
I have 2 Models: Projects and Members, each one with a form. I was able to add to the URL the number of the project (id) this:
class PageCreate(CreateView):
model = Page
form_class = PageForm
success_url = reverse_lazy('members:create')
def get_success_url(self):
return reverse_lazy('members:create', args=[self.object.id])
When I finish of filling the Project form, it redirects the page to the Member form.
What I want to do is to extract the ID of the Project from the URL and use it in the Member form. I cannot think any other solution.
Currently I have a Selection list to select the Project in the Member form but I want the Project loaded as soon as is created.
I am using the CreateView in the models for both Projects and Members. This is the view for MemberCreate
#method_decorator(login_required, name='dispatch')
class MemberCreate(CreateView):
model = Member
form_class = MemberForm
success_url = reverse_lazy('pages:pages')
Only attempt I had to visualize the ID in the HTML was using
{{ request.get }}
To somehow get the value from the GET but I could not do it.
Url Parameters
import imp
from django.urls import path
from .views import PageListView, PageDetailView, PageCreate, PageUpdate, PageDelete, MemberCreate, MemberDelete, MemberUpdate
pages_patterns = ([
path('', PageListView.as_view(), name='pages'),
path('<int:pk>/<slug:slug>/', PageDetailView.as_view(), name='page'),
path('create/', PageCreate.as_view(), name='create'),
path('update/<int:pk>', PageUpdate.as_view(), name='update'),
path('delete/<int:pk>', PageDelete.as_view(), name='delete'),
], 'pages')
members_patterns = ([
path('create/<int:pk>', MemberCreate.as_view(), name='create'),
path('update/<int:pk>', MemberUpdate.as_view(), name='update'),
path('delete/<int:pk>', MemberDelete.as_view(), name='delete'),
], 'members')
Stuff parsed from the URL ends up in the view's self.kwargs. From
return reverse_lazy('members:create', args=[self.object.id])
you pass through an URL like
path( 'create/<int:project>', MemberCreateView.as_view(), name='create' )
and in the view, the id is now self.kwargs['project']. (note, an URL can specify multiple named variables separated by slashes, it's not limited to just one). You typically then use
project = Project.objects.get( pk = self.kwargs['project'] )
Request.GET is something different: it's where the dict encoded as a querystring goes. If your client supplies
http://server/app/foo?bar=27&baz=hello
then when you arrive in the view which handles app/foo, request.GET contains
{ 'bar':'27', 'baz':'hello' }
(actually it's a QueryDict, not a Python dict, which has some subtle diffreernces. Consult the Django documentation. The main difference is that the values attached to keys can be multi-valued, for a querystring like ?bar=27&bar=54&bar=silly
This is my Django model:
class M(models.Model):
a = models.IntegerField()
b = models.IntegerField()
This is the serializer:
class MSerializer(ModelSerializer):
class Meta:
model = M
fields = ['a', 'b']
I would like to be able to implement these REST APIs:
127.0.0.1:8000/m/ (GET list of all elements, POST new element)
127.0.0.1:8000/m/:id/ (GET details of element with id id)
127.0.0.1:8000/n/:a/m/ (GET all elements with a specific a field)
So far this is the view and urls that I implemented:
class MViewSet(ModelViewSet):
queryset = M.objects.all()
serializer_class = MSerializer
router = DefaultRouter()
router.register(r'm', MViewSet)
urlpatterns = [
path('', include(router.urls)),
]
However, in this way the third use case is not working. How can I modify my code to make the third case work? I'd prefer to use as few lines of code as possible (i.e., I would like to use some Django built-in functionalities).
Since it look like you want the 3rd endpoint on another root (possibly another app name n), I'll implement it is a standalone API view, and not as an action on a viewset (although both options are possible)
class FilteredMListView(ListAPIView):
serializer_class = MSerializer
def get_queryset(self):
return M.objects.filter(a=self.kwargs["a"])
Then you register it to the router using:
urlpatterns = [
path("n/<str:a>/m/", FilteredMListView.as_view())
]
For your 3rd case, I would use a ListAPIView, overriding the get_queryset method to filter by the passed value for a. The idea is that when get_queryset method is invoked, and with as many other filters you'd like to implement, the condition for a is always present. Since the value for a will be in the url, it is mandatory, and you always have in in the view's kwargs. Would look like this:
urls.py
router = DefaultRouter()
router.register(r'm', MViewSet)
urlpatterns = [
path('', include(router.urls)),
path('<a>/m', AValuesListApiView.as_view()
]
views.py
class AValuesListApiView(generics.ListAPIView):
queryset = M.objects.all()
serializer_class = MSerializer
def get_queryset(self):
return super().get_queryset().filter(score=self.kwargs["score"])
url.py
urlpatterns = [
path('api_doc/', schema_view),
path('admin/', admin.site.urls),
# regex for swagger creation
path(r'^request.GET.get(‘tag’)&request.GET.get(‘order_by’)', views.QuestionList.as_view()),
# path(r'^?tag={tag}&order_by={name}', views.QuestionList.as_view()),
]
This is mu url.py file and i a trying to input "tag" and "order_by" in url in swagger but it's not working? i have tried above url options
In the last one "?" is not reconized by url.
The query string [wiki] is not part of the path. So you can not capture or check this in the urlpatterns. Your path looks like:
path('', views.QuestionList.as_view())
In your view, you can then filter the queryset accordingly:
from django.views.generic.list import ListView
from app.models import Question
class QuestionList(ListView):
model = Question
def get_queryset(self, *args, **kwargs):
qs = super().get_queryset(*args, **kwargs)
if 'tag' in self.request.GET:
qs = qs.filter(tag__id=self.request.GET['tag'])
if 'order_by' in self.request.GET:
qs = qs.order_by(self.request.GET['order_by'])
return qs
That being said, you are introducing a security vulnerability by allowing arbitrary ordening.
It furthermore might be worth to take a look at django-filter [GitHub] to do filtering based on a QueryDict in a more declarative way.
My django application has a namespace defined in the app_name variable, in urls.py.
It seems like this namespace needs to be specified in the view_name argument of HyperlinkedRelatedField for HyperlinkedRelatedField to successfuly retrieve the relevant url router.
To avoid repeating this namespace, I'd like to import the namespace into the serializers module. However I get an import error when doing so.
extract from my app/urls.py:
...
app_name = 'viewer'
...
api_router = DefaultRouter()
api_router.register('year', api_views.YearViewSet, 'api_year')
api_router.register('month', api_views.MonthViewSet, 'api_month')
...
urlpatterns = [
...
path('api/', include(api_router.urls)),
]
api_views.py
...
class YearViewSet(mixins.ListModelMixin, mixins.RetrieveModelMixin, GenericViewSet):
queryset = Year.objects.all().order_by('-date')
serializer_class = YearSummarySerializer
lookup_field = 'slug'
def retrieve(self, request, *args, **kwargs):
instance = self.get_object()
serializer = YearDetailSerializer(instance=instance)
return Response(serializer.data)
#action(detail=True)
def months(self, request, *args, **kwargs):
serializer = YearMonthsSerializer(instance=self.get_object(), context={'request': request})
return Response(serializer.data)
...
serializers.py
...
from .urls import app_name
...
class YearMonthsSerializer(serializers.HyperlinkedModelSerializer):
month_set = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name= app_name + ':api_month-detail',
lookup_field='slug'
)
class Meta:
model = Year
fields = ['month_set']
...
When I manually enter the app_name ('viewer') the serializer works as intended, however when I try to import app_name from .urls, python throws an ImportError
ImportError: cannot import name 'app_name' from 'cbg_weather_viewer.viewer.urls'
I don't understand why I get this import error as I already use relative imports models, views, etc. successfully.
I don't understand what I am doing wrong. What should I do instead?
Edit
I understand that I cannot include url as it will create a circular reference.
After doing some research, it seems that the request contains the app_name value as documented here: https://docs.djangoproject.com/en/2.2/ref/urlresolvers/#django.urls.ResolverMatch
However, I don't know how to access the request directly in the YearMonthSerializer class. I could retrieve it using self.request in a class method, but not directly from the class itself. Any suggestion?
Temporary solution
I have deported the app namespace to the apps.py module and imported it both urls.py and serializers.py as follow:
apps.py
app_namespace = 'viewer' # Used in urls
urls.py
from .apps import app_namespace
app_name = app_namespace
serializers.py
from .apps import app_namespace
def get_view_name(view_name):
return f'{app_namespace}:{view_name}'
...
month_set = serializers.HyperlinkedRelatedField(
many=True,
read_only=True,
view_name= get_view_name('api_month-detail'),
lookup_field='slug'
)
put namespace in the path when you include URLs like this
without namespace don't retrieve app_name
try this
urlpatterns = [
...
path('api/', include(api_router.urls,namespace="viewer")),
]
The documentation of the restframework states following, which worked for me:
You may use include with an application namespace:
urlpatterns = [
path('forgot-password/', ForgotPasswordFormView.as_view()),
path('api/', include((router.urls, 'app_name'))),
]
Or both an application and instance namespace:
urlpatterns = [
path('forgot-password/', ForgotPasswordFormView.as_view()),
path('api/', include((router.urls, 'app_name'), namespace='instance_name')),
]
See Django's URL namespaces docs and the include API reference for
more details.
Pay attention to the double parenthese with the include statement in the first example:
include((router.urls, 'app_name'))
I am new to Django Class based views. I am trying to make a simple view to get details of a post.
My views.py:
from django.views.generic import ListView, View, DetailView
class GenreDetail(DetailView):
model = Post
template_name = "post.html"
My urls.py:
urlpatterns = [
url(r'(?P<post_id>[^/]+)', GenreDetail.as_view(), name = 'post'),
url(r'(?P<post_id>[^/]+)/(?P<slug>[-\w]+)$', GenreDetail.as_view()),
]
Error that I get:
AttributeError at /2/memoirs-of-a-geisha-by-arthur-golden
Generic detail view GenreDetail must be called with either an object pk or a slug.
So the pk or slug is not passed to the Generic Detailview. How do I pass that ? I assume from url it can pick up but it's not.
url patterns are checked in the order you define them
so here:
urlpatterns = [
url(r'(?P<post_id>[^/]+)', GenreDetail.as_view(), name = 'post'),
url(r'(?P<post_id>[^/]+)/(?P<slug>[-\w]+)$', GenreDetail.as_view()),
]
...the first pattern is getting matched (because it does not end with $ so the extra segment is just ignored)
...and that pattern only passes a single keyword arg
Generally it is a bad idea to have multiple url patterns pointing to the same view. If possible you should try and make a single regex (eg using optional groups) which handles the various cases of the url for a particular view. It's more explicit that way.
On the other hand, simply reversing the order of your patterns to put the more explicit one first would also work and be correct (this is the Django rule of urlpatterns!)
urlpatterns = [
url(r'(?P<post_id>[^/]+)/(?P<slug>[-\w]+)$', GenreDetail.as_view()),
url(r'(?P<post_id>[^/]+)', GenreDetail.as_view(), name = 'post'),
]
As #ozgur mentions you also need to tell the view to use post_id instead of pk by setting pk_url_kwarg
If you want to fetch details using either post_id or slug then your urls should be like this
url(r'post/(?P<post_id>\d+)/$', GenreDetail.as_view(), name = 'post_detail'),
url(r'post/(?P<slug>[-\w]+)/$', GenreDetail.as_view(), name = 'post_detail_slug'),
And your view should be like this
from django.views.generic import DetailView
class GenreDetail(DetailView):
model = Post
template_name = "post.html"
pk_url_kwarg = "post_id"
slug_url_kwarg = 'slug'
query_pk_and_slug = True
For more details please read the docs.
The problem is that you have to tell DetailView that it should use post_id keyword in the URL instead of default ones pk or slug in order to get the object that will be displayed.
This can be done by setting pk_url_kwarg attribute:
(Your url definition is also wrong, always end your url definitions with $. Below is the corrected version)
url(r'(?P<post_id>\d+)$', GenreDetail.as_view(), name = 'post'),
url(r'(?P<post_id>\d+)/(?P<slug>[-\w]+)$', GenreDetail.as_view()),
The following urls will match given the url patterns above:
/2
/2/memoirs-of-a-geisha-by-arthur-golden
from django.views.generic import DetailView
class GenreDetail(DetailView):
model = Post
template_name = "post.html"
pk_url_kwarg = "post_id"
Alternatively, you can just change post_id to pk in your url so you don't have to touch anything in your view:
url(r'(?P<pk>\d+)$', GenreDetail.as_view(), name = 'post'),
url(r'(?P<pk>\d+)/(?P<slug>[-\w]+)$', GenreDetail.as_view()),
Using path:
from django.urls import path
from . import views
urlpatterns = [
path('<pk>/', views.GenreDetail.as_view(), name="post")]
For slug:
path('<slug:slug>/', views.GenreDetail.as_view(), name="post")