django-urls

Get django object id based on model attribute

我们两清 提交于 2019-12-31 17:57:50
问题 I have a basic model named "Places" which has this view: def view_index(request, place_name): The user will access that view with a URL like this one: http://server.com/kansas "kansas" is a value stored in a field named "name" inside the model "Places". The problem is that I can't figure out how to obtain the object id based just on the object name. Is there a way to do this? 回答1: Like this: place = Places.objects.get(name='kansas') print place.id 回答2: Since you only want id , you should only

Django - DRF - dispatch method flow

谁都会走 提交于 2019-12-31 07:26:06
问题 I am working with DRF to build an API and I used a master class to do some validations to my class based views: class MasterClass(APIView): def dispatch(self, request, *args, ** response = super(FaveoAPIView, self).dispatch(request, *args, **kwargs) # I call super because I need access to request data. # <some validations here> # Return a JsonResponse with an error message if validations fails class MyView(MasteClass): def post(self, request, *args, **kwargs): # At this point request is:

“Reverse for … not found” - but there is?

帅比萌擦擦* 提交于 2019-12-31 01:49:04
问题 Ok, I've been banging my head against this for 30+ minutes, so here I am on stack overflow. I've got in a template: {% if user.is_authenticated %} <a href="{% url 'admin' %}"> Admin </a> {% endif %} And in urls.py: urlpatterns = [ path('admin', admin.site.urls, name = 'admin'), path('', views.index, name ='index'), ] Yet I still get: NoReverseMatch at / Reverse for 'admin' not found. 'admin' is not a valid view function or pattern name. Why is that? I even tested it out, and replaced admin

Redirect any urls to 404.html if not found in urls.py in django

家住魔仙堡 提交于 2019-12-30 18:06:53
问题 How can I redirect any kind of url patterns to a created page "404.html" page if it doesn't exist in the urls.py rather than being shown the error by django. 回答1: Make a view that'll render your created 404.html and set it as handler404 in urls.py. handler404 = 'app.views.404_view' Django will render debug view if debug is enabled. Else it'll render 404 page as specified in handler404 for all types of pages if it doesn't exist. Django documentation on Customizing error views. Check this

How Do I Use A Decimal Number In A Django URL Pattern?

☆樱花仙子☆ 提交于 2019-12-30 08:22:28
问题 I'd like to use a number with a decimal point in a Django URL pattern but I'm not sure whether it's actually possible (I'm not a regex expert). Here's what I want to use for URLs: /item/value/0.01 /item/value/0.05 Those URLs would show items valued at $0.01 or $0.05. Sure, I could take the easy way out and pass the value in cents so it would be /item/value/1, but I'd like to receive the argument in my view as a decimal data type rather than as an integer (and I may have to deal with fractions

No module named backends.default.urls

江枫思渺然 提交于 2019-12-29 06:41:11
问题 So I've installed django-registration through easy_install. I'm following a quick start guide and I'm trying to setup my urlConf, however it says module named backends.defauls.urls is not found. What might be the problem ? import registration (r'^accounts/', include('registration.backends.default.urls')), 回答1: Is the registration module in your PYTHONPATH ? 回答2: (not my solution, but since it was hidden in a comment) You need to use use include('registration.urls') , instead of include(

The included urlconf manager.urls doesn't have any patterns in it

百般思念 提交于 2019-12-29 05:44:23
问题 A solution: Found the following django snippet that seems to work fine (http://djangosnippets.org/snippets/2445/) from django.utils.functional import lazy from django.core.urlresolvers import reverse #Workaround for using reverse with success_url in class based generic views #because direct usage of it throws an exception. reverse_lazy = lambda name=None, *args : lazy(reverse, str)(name, args=args) Apparently, there is now a reverse_lazy function in django trunk. Update: This error has

Django 1.7: some_name() takes exactly 2 arguments (1 given)

倖福魔咒の 提交于 2019-12-25 18:32:28
问题 this is my view.py from django.http import HttpResponse import datetime def current_datetime(request): now = datetime.datetime.now() html = "<html><body>It is now %s.</body></html>" % now return HttpResponse(html) def hours_ahead(request, offset): offset = int(offset) dt = datetime.datetime.now() + datetime.timedelta(hours=offset) html = "<html><body>In %s hour(s), it will be %s.</body></html>" % (offset, dt) return HttpResponse(html) this is my urls.py from django.conf.urls import patterns,

I have no idea what my error means: NoReverseMatch at /sssss/ Reverse for '' with arguments '(9, 19)' and keyword arguments '{}' not found

喜你入骨 提交于 2019-12-25 14:27:12
问题 I'm using django-favorites for follow/unfollow strategy. https://bitbucket.org/last_partizan/django-favorites/overview Problem is this might be wrote for django lower than 1.7 maybe and I'm using django 1.8. I fixed most of errors but now I get NoReverseMatch at /sssss/ Reverse for '' with arguments '(9, 19)' and keyword arguments '{}' not found. 0 pattern(s) tried: [] I have no idea what this is or how to fix this. It says it's coming from fav_item.html,which is part of app. from this line {

Django Template not loading

为君一笑 提交于 2019-12-25 14:09:35
问题 I have a project named 'src' and app named 'app' and i have a template folder inside my app. And inside my template folder i have another folder named pages and my html pages(base.html and view.html) are resides there. My view.py is below from django.shortcuts import render from django.utils import timezone # Create your views here. def home(request): return render(request, "pages/base.html", {}) and urls.py from django.conf import settings from django.conf.urls import include, url from