Adding urls/views to a model - Django Oscar? - django

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()

Related

Using double url tag in django

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.

Django optional URL captured value for rest APIView

For following Django code using django_rest_framework:
class PollMessageView(generics.ListAPIView):
serializer_class = MessageSerializer
lookup_url_kwarg = 'count'
def get_queryset(self):
count = self.kwargs.get(self.lookup_url_kwarg)
queryset = Message.objects.all()[:count]
return queryset
urlpatterns = [
path('poll_message/<int:count>', PollMessageView.as_view(), name="poll_message"),
]
How do I make the count parameter optional in the URL pattern? For example, if I visit /poll_message/ without a count, it would still call the PollMessageView (and I can set a default number for count if it's missing)?
create a new path as ,
urlpatterns = [
path('poll_message/', PollMessageView.as_view(), name="poll_message-wo-count"),
path('poll_message/<int:count>', PollMessageView.as_view(), name="poll_message"),
]
# URLconf
from django.urls import path
from . import views
urlpatterns = [
path('blog/', views.page),
path('blog/page<int:num>/', views.page),
]
# View (in blog/views.py)
def page(request, num=1):
# Output the appropriate page of blog entries, according to num.
...
In detail reference - https://docs.djangoproject.com/en/3.0/topics/http/urls/#specifying-defaults-for-view-arguments
You can specify defaults for view arguments, but the example on the website is for functional view, not sure how would that work for class based view or in this case, the rest framework class based view.
However, you can add another path with extra options to achieve the same thing
path('poll_message/', PollMessageView.as_view(), {
"count": 5
}, name="poll_message"),

Django Rest Framework Reverse() Method Fails

My main urls.py is located here ahlami -> ahlami -> urls.py
urlpatterns = [
path('admin/', admin.site.urls),
path('api/accounts/', include('accounts.api.urls')),
]
My accounts app urls.py is located here ahlami -> accounts -> api -> urls.py
urlpatterns = [
path('users/<int:pk>/', views.UserView.as_view(), name='user-detail')
]
One of my accounts views.py returns
token = Token.objects.create(......)
return Response(data=AnonymousUserTokenResponseSerializer(instance=token).data)
My token model has three fields only. For simplicity, I listed one field below
class Token(rest_framework.authtoken.models.Token):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE
AnonymousUserTokenResponseSerializer is linked to the Token model and returns three json attributes
class AnonymousUserTokenResponseSerializer(serializers.ModelSerializer):
user_id = serializers.ReadOnlyField(source='user.id')
user_url = reverse(viewname='user-detail')
class Meta:
model = Token
fields = ('key',
'user_id',
'user_url')
AnonymousUserTokenResponseSerializer fails because it can't identify reverse()
user_url = reverse(viewname='user-detail')
python manage.py runserver throws the error below because of the line above
django.core.exceptions.ImproperlyConfigured: The included URLconf
'ahlami.urls' does not appear to have any patterns in it. If you see
valid patterns in the file then the issue is probably caused by a
circular import.
My settings is located here ahlami -> ahlami -> settings -> base.py and base.py has this
ROOT_URLCONF = 'ahlami.urls'
I expect to get an output that looks like but couldn't because of the error above.
{
"key": "891e388399f2fcae016fe6887107034239041478",
"user_id": 29,
"user_url": http://localhost/api/accounts/users/29
}
How can I resolve this error and make reverse() work?
django.core.exceptions.ImproperlyConfigured: The included URLconf
'ahlami.urls' does not appear to have any patterns in it. If you see
valid patterns in the file then the issue is probably caused by a
circular import.
Use serializers.HyperlinkedIdentityField
class AnonymousUserTokenResponseSerializer(serializers.ModelSerializer):
user_id = serializers.ReadOnlyField(source='user.id')
user_url = serializers.HyperlinkedIdentityField(
view_name='user-detail',
source='user.id',
lookup_field='pk'
)
class Meta:
model = Token
fields = ('key', 'user_id', 'user_url')

Django REST framework RetrieveAPIView gets empty "id" parameter and returns 404 error

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}/

Why M I getting a 404 in my django project?

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.