I am using Django and I have the following URL in my project's urls.py file.
(r'^user/(?P<username>[\w_\-\.]+)/my_app/', include('my_app.urls')),
(r'^user/(?P<username>[\w_\-\.]+)/my_other_app/', include('my_other_app.urls')),
...
The goal is to have an application that uses the username of a user e.g. a profile application where every user has a profile page. Only one view in this application needs to have the username in the URL (the one that renders the profile page), but all the views must take a username parameter even if they don't do anything with it.
I suspect that this usage of URL parameters is wrong because it forces every view of my_app to take username as a parameter (because it's passed as from the URL dispatcher). To me it doesn't make sense for an external component (the project's urls.py file) to tell my_app's URLs what parameters to take.
Is this usage correct? If not, how should I do this?
It does seem kind of odd.
Could you not switch your apps to look for the username parameter instead? Under my_app.urls and the latter, just have the username field in the lookup on the fields you need.
That way your structure looks more like:
/user/my_app/friends/hekevintran/
/user/my_other_app/connections/bartek/
Which works just as well and doesn't force your apps to rely on the context of your root project for the username.
Related
I am making a project where each client have their own app. This is because they have similar pages but not exactly the same so I think it's a good approach(I may be wrong) to just copy one app for each new client. I have not tried it yet, I am still planning for it. I see one problem with the view site link in the admin. I will let the clients use the admin. How can I set the view site link to the main page for the client? One way to solve it would be to leave it as is and have a function checking their user name and redirecting to the right app. But is there any other way to solve this problem?
I don't think it will be a good idea to have applications generated for users as that's too much once you reached a specific amount of users.
they have similar pages but not exactly the same
Then what you should do is to, after getting the user in your view, pass in a different context to your template. Something like:
def my_view(request):
# First assign different context to different users
context = {'data': 'whatever each user gets', 'other': 'put in more than 1 data',}
return render(request, 'myapp/index.html', context)
I will let the clients use the admin
That's not a good idea as the clients MUST be a superuser to view the admin site. Otherwise, you need to change the permissions of a superuser, make a separate superuser if you are the maintainer of the site, and all sorts of trouble. Just take some time and make your own templates.
How can "http://my-domain.com/users" return a unique profile page for each user?
RedirectView in Django is normal view. It can take parameters from URL dispatcher, do some logic and decide where to redirect, based on that parameters (or even something more).
New URL can be fetched from database, built using parameters from dispatcher or even randomized.
You can also use old, function based view and simply return HttpResponseRedirect.
I couldn't find this info in the Django docs, but I'm sure it is there, I'm just very new and don't know what terms/etc to search on.
How are Django page templates assigned to each page?
I have a login to a Django site, and also SFTP access to the site. I don't think my Django login is a superuser/full-admin though because the interface seems pretty limited compared to other CMS systems. I can edit pages, posts and the media library, but I don't see anything that says how each page is assigned a template.
For example, I have this file /mysite/templates/pages/index.html
I know that template is being used for the home page because it has all of the content that is specific to the home page on it, and changes I make show up on the home page.
I tried copying that file to test.html, but when I browse to test.html in my browser, I get a 404 error (I also get that error if I go to index.html). So there must be something else that maps a template to a page, but I'll be dambed if I can find it. Will I need more access to the admin area, or can I do something with SFTP? I also have SSH access but wasn't able to follow any of the steps online to create a new superuser account for me, for Django.
Edit: Thanks for both answers, after I work through this I'll accept whichever helped the most. I do not have a views.py file, but I think it might be using an extra module for this routing, I have this in my urls.py file:
urlpatterns = patterns("",
("^admin/", include(admin.site.urls)),
url("^$", "mezzanine.pages.views.page", {"slug": "/"}, name="home"),
("^", include("mezzanine.urls")),
)
Is this "mezzanine" something different which changes the answer (location of views.py or list of views)?
url.py is the file that maps the urls to methods that return rendered templates. In essence you define the url and a method and when someone goes to that url, that method gets called which returns a HTTP response with the rendered template. This map is called urlpatterns. In the following example when someone goes to yourwebsite/blog then in the blog apps, view.py, page method is called, which will use a template and render that with specific information.
urlpatterns = patterns('',
url(r'^blog/$', 'blog.views.page'),
url(r'^blog/page(?P<num>\d+)/$', 'blog.views.page'),
)
Have a look at this link.
https://docs.djangoproject.com/en/dev/topics/http/urls/
Django uses urls.py files to map paths to views. This match is resolved using regular expressions. When a match is found, Django executes the associated view (usually inside views.py). The view is in charge to render the template required for the path (by finding it on the server's hard disk and loading it).
All aforementioned means that there's no direct association between a url path (i.e www.example.com/path/to/page) and a file on the server's hard disk (i.e /server/path/to/page). It's all performed dynamically by Django's engine when a request comes in.
If you want to know which view is gonna be generated for a specific path, follow the regexs at urls.py until you find the path you're looking for. Then open the view for that url and see inside which template it is rendering.
Reading doc's URL Dispatcher is a good point to start learning about this.
Hope this helps!
I'm trying to create an account settings page for logged-in users. One of the things users should be able to do is to change their password.
I'm using django-registration and it provides a password change form at site/accounts/password/change by default, and it works. However, I want this function to be available at an Account Settings page instead, along with other administrative functions.
I first tried copying the template code, but it did not work because it includes a special form to create the inputs for the passwords (to handle validation). I don't know how to include this form in my own template.
How can I recreate these forms in my own Account Settings template?
This is the default password change template. I want to reuse form.oldpassword and the others in a separate template.
Django-registration doesn't implement its own password change view, it reuses the one included in Django (django.contrib.auth.views.password_change). It is hooked in through registration.auth_urls which is included in the default and simple registration backends.
By default the view uses django.contrib.auth.forms.PasswordChangeForm (which can be overridden through the password_change_form parameter).
When you only reuse the form (which you can do of course, just import the above form in your custom view), you should be aware that you would still be missing the whole view logic. So unless you have a more complex view in mind, you should consider to reuse it and just override the registration/password_change_form.html and registration/password_change_done.html templates.
Django tutorials everywhere use constant-set application name all around - in urls file, in HTML templates, in views. But if I want to distribute an application and let the user sets it name (i.e. its URL postfix on http://server.com/appname) - how can I do?
I must have some common name setting then in configuration, but how to work it for template files, etc?
The only thing that matters with reference to the URL is the app's urlconf. As long as you do your imports via the app's package, e.g. appname.models, appname.views, etc., all consumers of your app will have to do after installation is add it to their INSTALLED_APPS and include() it in their urlconf. Everything else will be found by Django provided they are in their default locations.