Django admin site with url parameters - python

I have a Django project with several applications, and I want to add the Django admin site for one of these.
The problem I have is that the main urls.py file has
url(r'^tools/(\w+)/', include('tools.myapp.urls')),
and in my myapp.urls I have added
url(r'^admin/', include(admin.site.urls)),
The problem is that the parent part of the url matching uses a parameter, which is usually passed in the template (that's the application name) like so
{% url "my_view_function" request.info.appname %}
But the default Django template obviously don't include that extra parameter, when calling
{% url 'admin:logout' %}
thus leading to a NoReverseMatch exception.
How can I have the admin site working?

If you just want to have admin run under any tools/whatever/admin URL, you can add this line before the tools.myapp.urls import in your main urls.py:
url(r'^tools/\w+/admin/', include(admin.site.urls)),

Related

DRF does not reverse Django url pattern: NoReverseMatch

There is a complex app (not possible to just paste the code). Going to try to explain.
Django
There is a urls.py file from the native Django app. The urlpatterns defines and register its urls. The ^foo/ defines a group of related urls and the foonamepsace.
urlpatterns = patterns('',
...
url(r'^foo/', include('foo.urls', namespace='foonamespace')),
...
Now there is a method generate_report which does some logic inside and then uses render_to_string to return the HTML:
def generate_report(..):
...
return render_to_string('foo/foo_report.html', args)
Everything works inside the app, the url get reversed successfully.
Django Rest Framework (DRF)
Now there is a DRF implementation and one of its resources is supposed to return a report in a binary format.
class PDFReportViewSet(APIView):
renderer_classes = (BinaryFileRenderer, )
def get(..):
...
pdf = generate_report() # <-- fails with NoReverseMatch
...
return response
Problem
The ViewSet calls the generate_report, however one gets an error when trying to parse the HTML:
NoReverseMatch: foonamespace' is not a registered namespace
Question
Any clues why DRF cannot reverse the namespcae/url from the the core of Django app? How to make sure DRF can reverse a namespace from the core urls.py urlpattern?
Added
After investigation, inside the foo_report.html any usage of the url, for example {% url 'foonamespace:123' %} or {% url 'barnamespace:123' %} produces the error - only if ran from the DRF (running the same page using native Django works fine).
URLS
foo.urls.py
from django.conf.urls import patterns, url
from foo.views import (FooListView, FooDetailView...)
urlpatterns = patterns('',
url(r'^$', FooListView.as_view(), name='foo_list'),
url(r'^(?P<pk>\d+)/$', FooDetailView.as_view(), name='foo_details'),
Important note. The app is served at some.domain.com/, while the REST is served from some.domain.com/rest. So may be this way /rest just don't include anything because it is a parent of the root (which includes the foo.urls.py)
I was managed to resolve my issue with the help from #dirkgroten. It was difficult to see the problem without looking at the source code.
Solution
Updated the routers.py file:
urlpatterns = router.urls
urlpatterns += patterns('',
url(r'^foo/', include('foo.urls', namespace='foonamespace')),
)
Explanation
Basically, the app was serve from the root url / while the rest was served from /rest. The DRF router simply didn't include any of the root routes. Adding them manually like it is shown in solution resolved the problem and made foonamespace visible for all DRF elements.

Unable to resolve URL string for path using name with reverse

I have been spending the better part of my week learning Django to build my website and I've stumbled upon an issue I can't seem to get working. I wish to resolve a URL string for a path by including with that path a particular name and then being able to reference that name down the line. This works fine until I change the route in path to use the include method.
First Attempt:
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls'), name='testapp'),
path('about/', views.about, name='about'),
]
Now when I call {% url 'about' %} from my template html file I get back '/about/' as expected but when I try and call {% url 'testapp' %} I get a NoReverseMatch exception instead of getting '/testapp/'. After digging through the documentation I stumbled upon this example that shows path with include using a namespace instead so I adapted the above a bit.
Second Attempt:
# from mysite/urls.py (adapted from before)
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls', namespace='testapp')),
path('about/', views.about, name='about'),
]
#from testapp/urls.py
from django.url import include, path
from . import views
app_name = 'testapp_name'
urlpatterns = [
path('', views.index, name='testapp_index'),
path('directory/', views.directory, name='testapp_directory'),
]
Now from the previous example I try using the namespace in lieu of a name {% url 'testapp' %} and I again get the same NoReverseMatch exception however using the namespace and name from my included url {% url 'testapp:directory' %} does work giving me '/testapp/directory/'.
I know there's some concept I'm not getting or something I'm overlooking but I'm just running around in circles at this point and would really appreciate any help somebody could afford me.
If it helps I'm trying to get the path so that I can use it in a navigation bar to highlight the currently activated tab. I'm also not hardcoding the paths as I'm trying to keep it DRY although at this point if I can't get it done I might have to but I'm hoping someone has a much better idea of what they're doing and could point me in a helpful direction. I appreciate all assistance and thank you!
The problem is that testapp is not a single view: it is an include(..), so it encapsulates a collection of views:
from django.url import include, path
from . import views
urlpatterns = [
path('testapp/', include('testapp.urls'), name='testapp'),
path('about/', views.about, name='about'),
]
It is not said that this collection contains a view at all, or it can contain multiple. Even if it only contains a single view, then it would be unsafe since you can later change your mind, and add an extra view.
If there are two or more views, then how will you decide what view (and therefore URL) to take? If the include(..) has two views: the "homepage" and the profile page, then this makes a significant difference.
You thus should refer to a real name, and whether you give the include(..) a namespace in the include(..) is irrelevant:
{% url 'testapp_name:testapp_index' %} <!-- first attempt -->
{% url 'testapp:testapp_index' %} <!-- second attempt -->
To reference the name of a real view.
{% url 'testapp_name:testapp_index' %}
or
{% url 'testapp_name:testapp_directory' %}
as you are using app_name in the urls file, you need to mention it with the name of the view

Django URL names use in Templates

Right now in my templates I hardcore the links in my navigation like the following:
`base.html`
About
Contact
<!-- etc -->
In my urls.py
urlpatterns = patterns('',
url(r'^about/$', TemplateView.as_view(template_name='pages/about.html'), name='about'),
url(r'^contact/$', TemplateView.as_view(template_name='pages/contact.html'), name='contact'),
)
Is there a way that in my base.html file reference the urlpatterns from urls.py so that anytime I change those, it reflects everywhere across my pages and templates?? Something like
<!-- what I would like to do -->
About
About
This is why url tag was invented:
Returns an absolute path reference (a URL without the domain name)
matching a given view function and optional parameters.
If you’re
using named URL patterns, you can refer to the name of the pattern in
the url tag instead of using the path to the view.
This basically means that you can use url names configured in the urlpatterns in the url tag:
About
Contact
This gives you more flexibility. Now any time the actual url of about or contact page changes, templates would "catch up" the change automagically.
Use Django's url template tag (docs here) with your named url pattern.
Example:
About
Contact
Note that the name you put in ' ' will be the name of the particular url pattern in your urls.py. Also, if this is the urls.py file in an app (that is, it gets routed initially from the base urls.py), you'll need to include the namespacing of the app.

Modifying and creating sites in djangos admin app

I have to include multiple changes to djangos admin panel, so I decided to fork the django admin app into my own django project.
As I was working with this admin app I recognized, that the site registration and template handling differs from the apps, that are normally created in django.
For instance, I want to keep the old admin index.html template and view, for backup and safety reasons but the landing page should be replaced by a custom page.
For that of course I need to change admin/templates/index.html and /admin/sites.py respectively.
I copied the old index function in admin/sites.py to old_index.py and created a old_index.html in the template folder.
But if I try to reference to old_index.html in my new index.html with
old index
I got an NoReverseMatch-Exception thrown. Unfortunately I did not found more information about how the django admin app itself register new views and sites, so an example or description would be helpful.
Creating separate views for the admin app in the distinct other apps in my project is no real option, due the high amount of changes, that need to be done.
The main urls.py
from django.conf.urls import patterns, include, url
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'django_project.views.home', name='home'),
url(r'^polls/', include('other_app.urls', namespace="other_app")),
url(r'^admin/', include(admin.site.urls)),
)
The admin app itself does not provide a urls.py file and the views.py is exactely the same as in django.contrib.admin I just copied the function index to a new function called old_index, referencing to a template old_index.html.
Maybe the point did not get so clear, as I expected. I copied the whole admin app in my project and want to add a custom defined site to it, regardless where. But I failed to understand how sites and views are registered in the admin app itself, because the way is different from the custom apps you create normally in django.
So, is it possible (and how) to add a custom site in the django.contrib.admin app?
I think you need to create your own AdminSite for custom purposes and keep default as it is. More about this you can find here: https://docs.djangoproject.com/en/1.6/ref/contrib/admin/#adminsite-objects and here https://docs.djangoproject.com/en/dev/ref/contrib/admin/#multiple-admin-sites-in-the-same-urlconf
Update:
You need to edit get_urls method of AdminSite class - add:
url(r'^$', wrap(self.old_index), name='old_index')
to urlpatterns variable. And rename old index method to old_index.

Cannot Get Haystack Tutorial Working

I have completed the tutorial for Haystack w/ Whoosh and re-read it about 6 times in the past few hours and cannot seem to figure out what my issue is. Basically, everything seemed to installed correctly, however when I visit the /search URL all I see is my base template without the search template (no input box/header/etc). This is my urls.py at the application level:
urlpatterns = patterns('',
url(r'^$', views.index, name='index'),
url(r'^search/', include('haystack.urls')),
url(r'a/list/$', views.admin_list, name='admin_list'),
url(r'a/list/(?P<list_id>\d+)/edit/$', views.admin_edit, name='admin_edit'),
)
The template lives in myapp/templates/search/search.html. I have added nothing to my views.py, because the tutorial didn't go over adding anything to that file. What am I missing here?
What is content of the data template file ( appname_text) file , if you are keeping document =true in search_index.py file your data template should have the fields that are to be searched .
data template for will be something like.
{{ object.foo }}
{{ object.bar }}

Categories

Resources