django-urls

Django nested URLs

雨燕双飞 提交于 2021-02-19 01:05:23
问题 How do I nest url calls in django? For example, if I have two models defined as class Post(models.Model): title = models.CharField(max_length=50) body = models.TextField() created = models.DateTimeField(auto_now_add=True, editable=False) def __unicode__(self): return self.title @property def comments(self): return self.comment_set.all() class Comment(models.Model): comment = models.TextField() post = models.ForeignKey(Post) created = models.DateTimeField(auto_now_add=True) With the following

Django nested URLs

只愿长相守 提交于 2021-02-19 01:04:15
问题 How do I nest url calls in django? For example, if I have two models defined as class Post(models.Model): title = models.CharField(max_length=50) body = models.TextField() created = models.DateTimeField(auto_now_add=True, editable=False) def __unicode__(self): return self.title @property def comments(self): return self.comment_set.all() class Comment(models.Model): comment = models.TextField() post = models.ForeignKey(Post) created = models.DateTimeField(auto_now_add=True) With the following

What is the urls.py regex evaluation order in django?

二次信任 提交于 2021-02-18 11:03:19
问题 I was having some problems with the regex in urls.py (I am a beginner to django as well as regexes in general) Here is my original urls.py url(r'^name/(?P<name>\w+)/$', 'course.views.name'), url(r'^', 'course.views.index'), And I was trying to access it using this: http://127.0.0.1:8000/name/blah/ My view looks like: def index(request): return HttpResponse("Hello, sam. You're at the course index.") def name(request, name): return HttpResponse("Hello, %s. You're at the course index." % name)

What is the urls.py regex evaluation order in django?

╄→尐↘猪︶ㄣ 提交于 2021-02-18 11:03:04
问题 I was having some problems with the regex in urls.py (I am a beginner to django as well as regexes in general) Here is my original urls.py url(r'^name/(?P<name>\w+)/$', 'course.views.name'), url(r'^', 'course.views.index'), And I was trying to access it using this: http://127.0.0.1:8000/name/blah/ My view looks like: def index(request): return HttpResponse("Hello, sam. You're at the course index.") def name(request, name): return HttpResponse("Hello, %s. You're at the course index." % name)

How can I redirect to a different URL in Django?

天涯浪子 提交于 2021-02-16 18:51:46
问题 I have two pages, one which is to display details for a specific item and another to search for items. Let's say that the urls.py is properly configured for both of them and within views.py for my app, I have: def item(request, id): return render(request, 'item.html', data) def search(request): #use GET to get query parameters if len(query)==1: #here, I want to redirect my request to item, passing in the id return render(request, 'search.html', data) What do I do to properly redirect the

Django 3.0: Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[^/]+)/$']

痞子三分冷 提交于 2021-02-11 16:01:47
问题 I have model named Book in models.py file. And this model has slug field to display details of books Books are being displayed in home.html template and product.html template is to display details of selected book. I really don't know much about slugs, and how they work. Models.py: class Book(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField('Title', max_length=255) authors = models.ManyToManyField(Author, related_name='books

Django 3.0: Reverse for 'product' with no arguments not found. 1 pattern(s) tried: ['product/(?P<slug>[^/]+)/$']

亡梦爱人 提交于 2021-02-11 15:59:40
问题 I have model named Book in models.py file. And this model has slug field to display details of books Books are being displayed in home.html template and product.html template is to display details of selected book. I really don't know much about slugs, and how they work. Models.py: class Book(models.Model): id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False) title = models.CharField('Title', max_length=255) authors = models.ManyToManyField(Author, related_name='books

No User matches the given query. Page Not found 404?

馋奶兔 提交于 2021-02-11 14:59:53
问题 In my blog website I want to open another user's profile on clicking one of the links. But it keeps on showing 404 error saying No user matches the given query. Here's that link in a base.html <a href="{% url 'blogapp:userprofile' username=view.kwargs.username %}">{{ view.kwargs.username }}</a> Here's my urls.py pattern for the function- path('userprofile/<str:username>/',views.userprofile,name='userprofile'), Here's my function views.py @login_required def userprofile(request,username): user

Add ?q=search+term in django url

≡放荡痞女 提交于 2021-02-11 14:04:17
问题 I want to integrate elastic search with django but first I need to get a nice parameter in url http://127.0.0.1:8000/search?q=search+term urls.py (of the view) urlpatterns = [ path('?q=', SearchIndexView.as_view(), name="search-index"), ] urls.py (of the app) urlpatterns = [ path('admin/', admin.site.urls), path('', include('home.urls')), path('u/', include('user.urls')), path('search', include('search.urls')), ] That is what I have so far but I cant figure out how to make it work. I want to

include() got an unexpected keyword argument 'app_name'

家住魔仙堡 提交于 2021-02-07 06:10:27
问题 i am making a blog application for my website with django-2.0 when i run server i see the following error File "C:\Users\User\Desktop\djite\djite\djite\urls.py", line 7, in <module> url(r'^blog/', include('blog.urls', namespace='blog', app_name='blog')), TypeError: include() got an unexpected keyword argument 'app_name' here is my main urls.py from django.contrib import admin from django.conf.urls import url,include urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^blog/', include(