I got two urls.py file
first one is for project urls.py
urlpatterns = [
path('admin/', admin.site.urls , name = 'admin'),
path('',homeView, name='home'),
path('post/',post, name='post'),
path('post/',include('post.urls'), name='posturl'),
path('student/',studentM, name = 'student' ),
path('student/',include('student.urls') , name='studenturl'),
]
second one is url for app
urlpatterns = [
path('index', post_index, name = 'index'),
path('details/' + '<int:id>', post_detail, name = 'detail'),
path('create', post_create, name = 'create'),
path('update', post_update , name = 'update'),
path('delete', post_delete , name = 'delete'),
]
I am trying to reach the adress like
:8000/post/details/5
but when i try to use in html
=>> {{post.id}}
=>> {{post.id}}
=>> {{post.id}}
None of these are not working I got 404 error.
How can I reach the link which I want
def post(request):
context = {
}
return render(request,'post/post.html',context)
def post_detail(request,id):
post = get_object_or_404(Post,id=id)
context = {
'post' : post
}
return render(request,"post/details.html",context)
Im assuming that you are tryng to access this url pattern:
path('details/' + '<int:id>', post_detail, name = 'detail'),
which can be written like this:
path('details/<int:id>/', post_detail, name='detail'),
if im correct, you should use this in your html a tag:
text for the link
also, try not to do this:
path('post/',post, name='post'),
path('post/',include('post.urls'), name='posturl'),
it is better practice creating the post view inside your app, and then your urls.py are going to look like this:
# this is the project urls file
...
path('post/', include('post.urls')), # dont need a name here, you are not going to reference this
...
# this is the app urls file
...
path('', post, name='post'), # this is going to accessed as localhost:8000/post
path('index', post_index, name = 'index'),
path('details/<int:id>', post_detail, name = 'detail'), # this is going to accessed as localhost:8000/post/details/the_id_of_the_post
path('create', post_create, name = 'create'),
path('update', post_update , name = 'update'),
path('delete', post_delete , name = 'delete'),
...
Hope I've helped, if you still have any questions, feel free to contact me.
Related
I'm using DRF DefaultRouter as follows.
# urls.py
router = DefaultRouter()
router.register('book', BookingViewSet, basename='booking')
router.register('book/<int:bk>/reservation', ReservationViewSet, basename='reservation')
urlpatterns = [
path('', include(router.urls)),
]
# view
class ReservationViewSet(viewsets.ModelViewSet):
serializer_class = ReservationSerializer
queryset = Reservation.objects.all() # for testing only
But when I visit the URL /book/1/reservation/ it says no url pattern found.
lending/ ^book/<int:bk>/reservation/$ [name='reservation-list']
lending/ ^book/<int:bk>/reservation\.(?P<format>[a-z0-9]+)/?$ [name='reservation-list']
lending/ ^book/<int:bk>/reservation/(?P<pk>[^/.]+)/$ [name='reservation-detail']
lending/ ^book/<int:bk>/reservation/(?P<pk>[^/.]+)\.(?P<format>[a-z0-9]+)/?$ [name='reservation-detail']
The current path, lending/book/1/reservation/, didn’t match any of these.
I'm using bk to capture book id.
That's because it implements the <int:bk> as regex, so without any interpretation. Probably the simplest way to do this is with two routers:
router1 = DefaultRouter()
router1.register('book', BookingViewSet, basename='booking')
router2 = DefaultRouter()
router2.register('reservation', ReservationViewSet, basename='reservation')
urlpatterns = [
path('', include(router1.urls)),
path('book/<int:bk>/', include(router2.urls)),
]
In the ReservationViewSet you can then for example filter with bk:
class ReservationViewSet(viewsets.ModelViewSet):
serializer_class = ReservationSerializer
def get_queryset(self):
return Reservation.objects.filter(book_id=self.kwargs['bk'])
Just wanted to add this to the accepted answer ... You can also do this with the same router, just embed the pk in the URL in regex form as the error message suggests:
# urls.py
router = DefaultRouter()
router.register('book', BookingViewSet, basename='booking')
router.register('book/(?P<bk>[^/.]+)/reservation', ReservationViewSet, basename='reservation')
urlpatterns = [
path('', include(router.urls)),
]
You can then access the bk argument in your view using self.kwargs['bk'].
I use Django 1.11 + Django REST Framework. I'm trying to get detailed data about distinct record using generic RetrieveAPIView from Django REST Framework and server returns "HTTP 404 Not Found" result, i.e. no data.
models.py file:
class TestPage( models.Model):
title = models.CharField( size = 120)
counter = models.IntegerField( default = 0)
def __unicode__(self):
return u'' + self.title
serializers.py file:
class TestPageSerializer(serializers.ModelSerializer):
class Meta:
fields = ('id', 'title', 'counter',)
model = models.TestPage
urls.py file:
urlpatterns = [
url( r'^admin/', admin.site.urls),
url( r'^api/', include( 'rest_framework.urls')),
url( r'^api/', include( 'testpages.urls')),
]
testpages/urls.py file:
urlpatterns = [
url( r'^$', views.TestPageList.as_view()),
url( r'^(?P<id>)\d+/$', views.TestPageDetail.as_view()),
]
and at last views.py file:
class TestPageList(generics.ListAPIView):
lookup_field = 'id'
queryset = TestPage.objects.all()
serializer_class = TestPageSerializer
class TestPageDetail(generics.RetrieveAPIView):
lookup_field = 'id'
queryset = TestPage.objects.all()
serializer_class = TestPageSerializer
# def get_object( self):
# print self.kwargs
If I request "http://127.0.0.1:8000/api/" for all records in the list, server returns all records. But if I want to get record by id using "http://127.0.0.1:8000/api/1/" for example, I get the empty result like in the photo below.
[![enter image description here][1]][1]
If I uncomment get_object()-function in last 2 lines, I can see {'id': u''} in terminal i.e. server gets empty parameter 'id'.
What wrong with this code?
The issue is your regular expression matching for id, you're putting \d+ outside of the matching group when it's actually what you want to match:
url(r'^(?P<id>\d+)/$', views.TestPageDetail.as_view())
By the way, to be good REST citizen, you shouldn't add the / at the end of a URL for a specific object.
Note: As mentioned by JPG, adding a name for the resource you're fetching (plural) would be proper RESTful:
url(r'^pages/(?P<id>\d+)$', views.TestPageDetail.as_view())
This way, you fetch page nr. 1 at /api/pages/1
The original problem was the closing parathesis (the regex expression), you were misplaced that.
urlpatterns = [
url(r'^$', views.TestPageList.as_view()),
url(r'^(?P<id>\d+)/$', views.TestPageDetail.as_view()),
^^^ here
]
Apart from that, You should change the urls patterns to a sensible form, as
#testpages/urls.py
urlpatterns = [
url(r'^test/$', views.TestPageList.as_view()),
url(r'^test/(?P<id>\d+)/$', views.TestPageDetail.as_view()),
]
then the list-api will be available on /api/test/ endpoint and the detail-api will be available on /api/test/{id}/
So I have a page which I want to render after the details section in my project. My urls.py is below:
from django.conf.urls import url
from . import views
app_name = 'main'
urlpatterns = [
url(r'^home/', views.home, name='home'), # Home page
url(r'incubators/$', views.incubators, name='incubators'), # Incubator list page
url(r'about/', views.about, name='about'), # Websie about page
url(r'results', views.result, name = 'result'), # For search function
url(r'incubators/(?P<incubator_id>[0-9]+)/', views.details, name = 'details'), # shows details of incubators
url(r'incubators/add-incuabtor/$', views.AddIncubator.as_view(), name = 'add-incubator'), # Adding Inc
url(r'/add-details/', views.AddDetails.as_view(), name = 'add-details'), #for additional details
url(r'news/', views.news, name = 'news'),
url(r'added/', views.added, name = 'added'), #your incubator will be added soon page
url(r'apply/', views.apply, name = 'apply'),
url(r'done/', views.done, name = 'done'),
url(r'location/', views.location, name = 'location'),
url(r'prediction/', views.prediction, name = 'prediction'),
url(r'^/locate/', views.locate, name = 'locate'),
]
I want to open a page (present in the details.html) which will be showing some info (in my case latitude and longitude) of that particular element.
Following is my views.py
def details(request, incubator_id):
inc = get_object_or_404(Incubators, pk = incubator_id)
details = Details.objects.get(pk = incubator_id)
return render(request, 'main/details.html', {'inc': inc, 'details': details})
def locate(request):
locate = get_object_or_404(Incubators, pk = incubator_id)
return render(request, 'main/locate.html', {'locate': locate})
But I am getting the following error:
NameError at //locate/
name 'incubator_id' is not defined
Request Method: GET
Request URL: http://127.0.0.1:8000//locate/
Django Version: 1.11.3
Exception Type: NameError
Exception Value:
name 'incubator_id' is not defined
Exception Location: C:\Users\my dell\Desktop\Project\junityme\main\views.py in locate, line 137
Python Executable: C:\Users\my dell\AppData\Local\Programs\Python\Python36-32\python.exe
Using the URLconf defined in Ekyam.urls, Django tried these URL patterns, in this order:
I think I am doing some mistake in making urls for this. Help would be appriciated.
http://127.0.0.1:8000//locate/ would be looking for a URL pattern that starts with "locate," but you don't have any URL patterns that start with locate. The URL should match the pattern, not the name you give it, or the view name.
I'm trying to use this link <a href="Annotation/{{ vod.id }}"> to load another page in my website base on primary key of videos. My url file is as follows:
urlpatterns = [
url(r'^$', ListView.as_view(queryset=Vod.objects.all().order_by("-date")[:25], template_name="Annotation/home.html")),
url(r'^(?P<pk>\d+)$', ListView.as_view(queryset=Vod.objects.get(pk=1).posts.all().order_by("-date"), template_name="Annotation/post.html")),
]
I get the standard 404 from the links generated using the aforementioned link.
Thanks!
edit: Added the base URLs
url(r'^admin/', admin.site.urls),
url(r'^Annotation', include('Annotation.urls')),
url(r'^Profile', include('Profile.urls')),
This is the URL for Profile.urls
url(r'^$', views.index, name='index'),
edit2: Changed URL and added view I'm trying to use.
url(r'^(?P<key>[0-9]+)$', views.post, name="post")
Here's views.post
def post(request, key):
try:
target_vod = Vod.objects.get(pk=key)
except Target.DoesNotExist:
raise Http404("Vod does not exist")
target_posts = Vod.objects.get(pk=key).posts.all().order_by("-date")
context = {'target_vod': target_vod, 'target_posts': target_posts}
return render(request, 'Annotation/post.html', context)
I have seperated the code and put it into a views.py. This should work.
urls.py
url(r'^(?P<pk>[0-9]+)$', AnnoList.as_view(), template_name="Annotation/post.html")),
Then in views.py:
class AnnoList(ListView):
template_name = 'Annotation/post.html'
def get_queryset(self):
self.vod = get_object_or_404(Vod, pk=self.args[0])
return Posts.objects.filter(vod=self.vod).all().order_by("-date")
I assuming here that you have two different tables and that Posts has a foreign key to Vod.
I want to add some urls/views to dashboard app. So I forked the app using oscar_fork_app. I have created an app.py file in forked dashboardapp folder. Also added an app.py inside catalogue sub-app inside dashboard app. Following this.
My #dashboard/app.py
from oscar.apps.dashboard.app import DashboardApplication as CoreDashboardApplication
class DashboardApplication(CoreDashboardApplication):
name = 'dashboard'
permissions_map = {
'index': (['is_staff'], ['partner.dashboard_access']),
}
index_view = get_class('dashboard.views', 'IndexView')
catalogue_app = get_class('dashboard.catalogue.app', 'application')
def get_urls(self):
urls = [
url(r'^catalogue/', include(self.catalogue_app.urls)),
]
return self.post_process_urls(urls)
application = DashboardApplication()
My #dashboard/catalogue/app.py
from oscar.apps.dashboard.catalogue.app import CatalogueApplication as CoreCatalogueApplication
class CatalogueApplication(CoreCatalogueApplication):
name = None
csv_upload_view = get_class('dashboard.catalogue.views',
'CSVUpload')
def get_urls(self):
urls = [
url(r'^csvupload/$',
self.csv_upload_view.as_view(),
name='csvupload'),
]
return self.post_process_urls(urls)
application = CatalogueApplication()
when I hit /dashboard it says
Page not found (404)
Request Method: GET
Request URL: http://localhost:8000/dashboard/
Using the URLconf defined in teashop.urls, Django tried these URL patterns, in this order:
^i18n/
^admin/
^accounts/reviews/$ [name='review-list']
^accounts/subscriptions/$ [name='subscriptions']
^accounts/storecredit/$ [name='storecredit']
^catalogue/
^basket/
^checkout/
^accounts/
^search/
^dashboard/ ^catalogue/
^offers/
The current URL, dashboard/, didn't match any of these.
Do I have to copy paste all the urls in oscar.apps.dashboard.app to my forked_app ? What's the way to extend in the right way ?
I had to add all the urls of the parent class.
class DashboardApplication(CoreDashboardApplication):
...
catalogue_app = get_class('dashboard.catalogue.app', 'application')
def get_urls(self):
urls = super(DashboardApplication, self).get_urls()
urls += [
url(r'^catalogue/', include(self.catalogue_app.urls)),
]
return self.post_process_urls(urls)
Similarly for the other app.py
class CatalogueApplication(CoreCatalogueApplication):
name = None
csv_upload_view = get_class('dashboard.catalogue.views',
'CSVUpload')
def get_urls(self):
urls = super(CatalogueApplication, self).get_urls()
urls += [
url(r'^csvupload/$',
self.csv_upload_view.as_view(),
name='csvupload'),
]
return self.post_process_urls(urls)
application = CatalogueApplication()