How to change default URL in Django Photologue - django

Is it possible to change default URL in Photologue? For example this URL
url(r'^photo/(?P<slug>[\-\d\w]+)/$', PhotoDetailView.as_view(), name='pl-photo')
I want to change on
url(r'^pictures/(?P<slug>[\-\d\w]+)/$', PhotoDetailView.as_view(), name='pl-photo')
In documentation I find only example how to override URL.

This is not a very elegant solution, but it will work:
In your project's urls.py file, you have already included Photologue's urls:
url(r'^photologue/', include('photologue.urls', namespace='photologue')),
What you can do is write a custom urls.py file, in which you copy-and-paste Photologue's urls.py file - but then you change 'photo' to be pictures.
Note: remember to change the import at the top to from photologue.views import ....
Then include your custom urls.py file in place of including the standard Photologue urls.py file.
Note: it's not a very elegant solution because you are duplicating code - and if you ever upgrade the Photologue version used in your project, you will need to check if the urls.py file has changed.

Related

Where is and how to edit the default Django homepage

Created a test demo project as below, how to edit the default Django homepage (or create my own homepage) that still using the same homepage url http://127.0.0.1:8000/?
Note: I don't want to create a new app and put the homepage under the new app, because that could change the homepage url from http://127.0.0.1:8000/ to http://127.0.0.1:8000/new_app.
You merely need to add the appropriate html template file and put the path into your project urls.py file. For example you could create a templates folder, under which you could add base.html or home.html or whatever you want (with the associated view), and then in urls.py you add:
path('', views.home),
You can get the default page back by adding the following in the urls.py file of your main app.
…
from django.views import debug
…
urlpatterns = [
…
path('', debug.default_urlconf),
…
]

Remove Django url namespace and serve app from root urls.py

I have a Django app which I created as a reusable app with its own urls.py. The app's urls.py is included from the project's root urls.py:
url(r'^app/', include('app.urls', namespace='app')),
The full urls are example.com/app/view1, example.com/app/view2 . I want to remove the /app/ part from the urls so the views can be called by urls directly following the domain name such as exmpale.com/view1, example.com/view2. I refer to the urls in templates and views as 'app:urlname'.
What's the best way to accomplish this? Ideally without having to edit all of my templates.

Django - split views into separate files while maintaining views.py

Hopefully this is a simple noob question. I'm working on my first large(ish) Django project. The first engineer on the project was following the default Django code layout. As things have grown we've recently split out models into their own directory with one file per model.
It's time to start doing the same for views. However, I don't want/need to do it all in one go. I'd like to just start moving things out of the default views.py one by one as I work on them. However, I'm having difficulty getting urls.py to work with both a views directory and views.py
Is this just going to cause a naming collision when I try to import 'views' in my urls.py file? Is the simple answer just to call "views" something else while I make the transition? Or just bite the bullet and do it all at once?
The direct answer is yes, you have a python naming collision. See for example Python Import Class With Same Name as Directory
You don't need to do it all as once though -- you can simply rename / move your views.py file or your new views directory. Moving the new directory would likely be easiest, then you won't have change your existing url routes.
There's nothing special about views.py files, as long as your urls.py points to the appropriate function, you can place them anywhere, and call them anything.
I recommend creating feature sub-directories within your app folder on your Django project. Then, simply import them from the new directory using the app.views namespace by using the from ... import * incantation. This will import any views without the module name prepended (e.g. what would normally be referenced by app_name.views.feature_one.view_class becomes app_name.views.view_class like you want). See below:
# app_name/views.py
from feature_one.views import *
from feature_two.views import *
# ... copy views from here
# new file: app_name/feature_one/views.py
# ... paste some views here
# new file: app_name/feature_two/views.py
# ... paste some other views here
# new file: app_name/feature_one/__init__.py
# ... this file can be blank. required for importing "feature_one" like a module
# new file: app_name/feature_two/__init__.py
# ... this file can be blank. required for importing "feature_two" like a module
Now, while your views will be spread across sub-directories, they are all imported with the same names into app_name.views so you can still reference the same names in urls.py despite having moved some views to other files.
Since you have divided models into their sub-apps, you can define urls specific to these sub-apps in the created sub directories and set up a url hierarchy. This is how your default urls.py will look like:
from django.conf.urls import include, patterns, url
urlpatterns = patterns('',
# ... snip ...
url(r'^comments/', include('your_website.comments.urls')),
url(r'^community/', include('your_website.community.urls')),
url(r'^contact/', include('your_website.contact.urls')),
# ... snip ...
)
where comments, community and contact are your newly created sub-apps/sub-directories. More info on how the url dispatcher works, here

Django auth inbuilt view cannot recognize my customized html file

I am trying to use django's inbuilt urls and views for the user authentication but have customized the html files, e.g. login.html registration/password_reset_form.html
I have imported the urls in my url.py
from django.contrib.auth import urls
and in the urlpatterns
url(r'^account/', include('django.contrib.auth.urls')),
in my views.py
from django.contrib.auth.views import *
(with no other view functions to handle the auth process)
in my registration file there are login.html password_reset_form.html password_reset_done.html...
The problem is that the django view login is recognizing my login.html under the registration file as it shows my customized log in page, but for the url account/password_reset/ the django password_reset view function cannot recongnize my password_reset_form.html, instead, it is using the django password_reset page.
Can anyone tell me where the problem could be and how to solve this problem?
I have read the django auth codes here https://github.com/django/django/tree/master/django/contrib/auth
and really want to use the django inbuilt url/views to make my project standard. Thank you very much.
The filesystem template loader uses settings.TEMPLATE_DIRS to specify where to look for templates, so make sure that you've added the directory containing registration/ to it (and that you've enabled the filesystem loader).

Django on APP Engine - error redirecting a URL path to an existing function in views.py (on urls.py)

I'm getting an error when I try to redirect a URL path to an existing function inside of a views.py file.
I realize where the problem is, but I cannot figure out how to solve it.
I have the following structure of folders on my project:
my_app_gae
app.yaml
main.py
settings.py
urls.py
my_app_django (<-- here is my django project)
dashboard
views.py
models
models.py
The problem comes here:
when I edit the urls.py file, when I try to redirect a specific URL path to an existing function inside of views.py (landing), I recieve the following error:
Request Method: GET
Request URL: http://localhost:8090/landing/
Exception Type: ImportError
Exception Value: No module named my_app_django
The value of my Python Path is: V:\Python~1\my_app_gae (the place where the structure of folders I wrote before is).
The url.py value that I'm trying to execute is:
from django.conf.urls.defaults import *
from my_app_django.dashboard.views import landing
urlpatterns = patterns(
'',
(r'^landing/$', landing),
)
If I copy the views.py file directly on the my_app_gae directory it works. The problem comes when the views.py file is inside of other directories.
Thanks a lot.
Regards
To be recognized as a Python package, you need empty files named __init__.py in each subdirectory.