django flatpages vs. named url's reverse - python

I am trying to set up flatpages for my django project. everything works fine. except this guy:
I have urls like:
url(r'^terms/$', TemplateView.as_view(template_name='terms.html'),
name='terms'),
which goes to a static html file. and in template i have:
terms
now I set up a new flatpage for terms page. Now I want to break the old static url terms (so that 404 get raised) so that my new flatpage will be found. my flatpages slug is also /terms/.
How can I "remove" the old slug so that my flatpage will be called?
.. i mean, without changing {% url 'terms '%}

You can specify the url to a single flatpage in your urls.py. In your case, all you need is:
url(r'^terms/$', 'django.contrib.flatpages.views.flatpage', {'url': '/terms/'}, name='terms')
See: https://docs.djangoproject.com/en/dev/ref/contrib/flatpages/#using-the-urlconf

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.

Incorporating existing html pages into Django

I have several hundred static html pages, with an index html page, that I need to bring into Django. I can't figure out the easiest way to do that, I know I'm missing something simple. Any advice?
Nothing fancy needed, just need to dump them in a directory and allow users to navigate to them.
You need to create a view and a url for each html template, iI'm going to put you a simple example here but it's highly recommended that you read Django's documentation or a tutorial :
First, you create a view in a views.py file :
from django.http import HttpResponseRedirect
from django.shortcuts import render
from django.views.generic import View
class LoadTemplateView(View):
template_name = ['thenameofyourdjangoapp/yourtemplatename.html']
#You put any code you may need here
def get(self, request, *args, **kwargs):
return render(request, self.template_name)
Then, you must create a url that reads that view in a urls.py file :
from django.conf.urls import patterns, url
#Here you import from views the view you created
from .views import LoadTemplateView
urlpatterns = patterns(
url(r'^myurl/$', LoadTemplateView.as_view(), name="load_template"),
)
Last, in your let's say home html template, you assign this url to a submit button in order to call it by the name you gave it on urls.py (load_template in this case) :
<html>
<body>
<div>
<a class="option-admin" id="id_go" href ="{% url 'yourdjangoappname:load_template' %}"> Go to template </a>
</body>
</html>
</div>
As I said anyway, it's better that you read the complete Django documentation as well:
https://docs.djangoproject.com/en/1.9/
If these are legacy static pages that you do not plan to touch again, then I'd leave django out of it. I'd put them all in a directory or subdomain and serve them directly from the server (probably nginx, maybe apache, whatever you're using). As a general rule, you don't want Django serving static assets, you want the proxy server serving them.
You COULD move them into Django and manage them like other Django static assets, as described in the Managing Static Files Documentation, but if they're already out there, then there's not a whole lot of advantage over just serving them as outlined above.
And finally, if you wish to fully integrate them into your Django site, then you should probably start with the template documentation.

Django admin site with url parameters

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

Get rid of default view in Satchmo / Django?

just curious what's the proper/transparent way to get plain 404 error instead of view for the pre-defined url ?
The default install of Satchmo handles /quickorder url with a cart view. I would like to completely disable handling for this url. So far I did get is the following url replacement in url.py at the top of store tree:
from django.conf.urls.defaults import *
from satchmo_store.urls import urlpatterns
from satchmo_utils.urlhelper import replace_urlpattern
replace_urlpattern(
urlpatterns,
url(r'^quickorder/$', handler404)
)
The handler404 is from django.conf.urls.defaults but it returns Django's own content instead of the shop's default 404 page like if you would ask for a non-existent page like http://www.google.com/quickorder_somenonsenseintheurl.
Any idea how to override this while avoiding modifications of default Satchmo installation ?

How do I use generic views in Django?

I am trying to use Django's generic views to make a user registration page. I have the following code in my app's urls.py
from django.conf.urls.defaults import *
from models import Ticket
urlpatterns = patterns('',
(r'^new/$', 'django.views.generic.create_update.create_object', { 'model': User } ),
)
Now when I go to that url I get the following:
TemplateDoesNotExist at /new/
auth/user_form.html
I have tried making a template to match it but I keep getting that message. Any advice?
Also, I assumed that this would make the template for me. What do I actually put in the template file when I make it?
EDIT: Coming from rails I was mixing templates and views. I am still stuck but I think I need to make a function in my view. Something like:
def user_form:
#stuff
You will need to create the template file using the format <app_label>/<model_name>_form.html. Place this template into the template directory as defined in TEMPLATE_DIRS in your settings file.
In the template file itself, you will need to use the context as defined by the standard ModelForm.
{{ form.name.label_tag }} {{ form.name }}
i don't think you need to create a view function. you need to provide a template (auth/user_form.html in this case, is also configurable) in your templates directory. The template needs to contain the necessary fields to create the new user object.
see the docs for this.
ans also check out this similar discussion

Categories

Resources