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
Related
I am having a problem with django allauth login page. I was able to complete the tutorial django all-auth, however I encountered a problem with the login page when I tried http://localhost/accounts/login/ nothing is showing but only a blank page.
And the tutorial does not show how to create the login page, so I guess that is the last step I should be working on.
What I did until now is this,
settings.py
INSTALLED_APPS = (
'django.contrib.sites',
'allauth',
'allauth.account',
'allauth.socialaccount',
'allauth.socialaccount.providers.google',
)
SOCIALACCOUNT_PROVIDERS = {'google': {'SCOPE': ['email'],
'AUTH_PARAMS': {'access_type': 'online'}
}
}
# Django all auth settings
AUTHENTICATION_BACKENDS = (
# Needed to login by username in Django admin, regardless of `allauth`
'django.contrib.auth.backends.ModelBackend',
# `allauth` specific authentication methods, such as login by e-mail
'allauth.account.auth_backends.AuthenticationBackend',
)
SITE_ID = 1
urls.py
url(r'^accounts/', include('allauth.urls')),
Also I already added a social application in the Django admin and it's required fields.
Thank you soo much, your help is much appreciated.
Piggybacking off of #bharat's post and for others stumbling across this post trying to figure out why nothing is being displayed...
django-allauth comes with templates that have all of the content that you already need and even a few templates that you may have not added yourself. But, as comes with the templates, comes the project-owners project structure.
If you want all of your django-allauth pages to use the content that it comes with, you will need to copy and paste the templates into your template folder and then update them to work with your project structure...
So, you will need to do to the typical installation process and then add all of the templates as well. I just can't find this documented anywhere. You can see all of the templates here.
EDIT: If you look closely at the top of the templates page it says 'Overridable templates' which clearly answers the issue that I had for hours. But, if you missed that as I did. There's your issue.
If you're not planning on using the django-allauth views though, you don't have to. You can totally implement social-authentication without using all of the views. So, it's up to you... But, once you implement them you will realize that it does save you an incredible amount of time in setting your future projects up. It's just a matter of getting it working the first time.
There are likely a couple of things you will have to change in the templates for everything to work.
Update the <title> block that is {% block head_title %}.
Update the {% block content %} to whatever you call your body block.
Import django-crispy-forms with {% load crispy_forms_tags %}.
Make all of your forms crispy.
If you're using all of the django-allauth views. Chances are you want an account view so you will need to create that as well because that does not come in the views.
But, you can totally just use the same pattern that django-allauth does and create something like accounts/<username>/
Having a look at this GitHub Repo would help you to create a login page:
https://github.com/pennersr/django-allauth/blob/master/allauth/templates/account/login.html.
You should also ensure that the login page has appropriate url-mappings on urls.py file.
Hope, that helps.
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.
I'm trying to combine Django with Jade, but I've had some problems.
I have model which is named About. This has a view like this:
def about(request):
return render_to_response('about.jade',{},RequestContext(request))
and in my urls I have:
url(r'about/', views.about),
But it provides an error that the Templates doesn't exist (and yes, it exists). Is it correct to write the url like this?
Any help would be appreciated!
If your getting the big Template does not exist page in your browser, this usually means that django cannot find where you have stored the your template file (irrespective of using jade).
If you ve created a djnago 1.6 project you need to add the following line to settings:
TEMPLATE_DIRS = [os.path.join(BASE_DIR, 'templates')]
Then create a templates directory inside your app (not project) directory, and put your template there.
I need to make a very simple modification -- require that certain views only show up when a user is not authenticated -- to django-registration default views. For example, if I am logged in, I don't want users to be able to visit the /register page again.
So, I think the idea here is that I want to subclass the register view from django-registration. This is just where I'm not sure how to proceed. Is this the right direction? Should I test the user's authentication status here? Tips and advice welcomed!
Edit
I think this is the right track here: Django: Redirect logged in users from login page
Edit 2
Solution:
Create another app, for example, custom_registration, and write a view like this (mine uses a custom form as well):
from registration.views import register
from custom_registration.forms import EduRegistrationForm
def register_test(request, success_url=None,
form_class=EduRegistrationForm, profile_callback=None,
template_name='registration/registration_form.html',
extra_context=None):
if request.user.is_authenticated():
return HttpResponseRedirect('/')
else:
return register(request, success_url, form_class, profile_callback, template_name, extra_context)
I had to use the same function parameters, but otherwise just include the test, and if we pass it, continue to the main function.
Don't forget to put this in your URLConf either (again, this includes some stuff about my custom form as well):
top-level URLConf
(r'^accounts/', include('custom_registration.urls')),
(r'^accounts/', include('registration.urls')),
custom_registration.views
from django.conf.urls.defaults import *
from custom_registration.views import register_test
from custom_registration.forms import EduRegistrationForm
urlpatterns = patterns('',
url(r'^register/$', register_test, {'form_class': EduRegistrationForm}, name='registration.views.register'),
)
As far as I remember django-registration is using function-based views, so you can not really subclass them. The approach I usually follow is "overwriting" the original views (without modifying the django-registration app of course). This works like this:
Create another app (you could call it custom_registration or whatever you want)
This app need to contain another urls.py and in your case another views.py
Copy the original register view code to your new views.py and modify it, add a pattern to your urls.py to point to this view (use the same url pattern as in django-registration for this view)
Put an include to your projects urls.py of your new app urls.py before your are including the original django-registration app. This could look like this for example:
urlpatterns = patterns('',
...
url(r'^accounts/', include('custom_registration.urls')),
url(r'^accounts/', include('registration.backends.default.urls')),
...
)
This simply works since the first matching url pattern for /accounts/register will point to your new app, so it will never try to call the one from the original app.
I am working on a site currently (first one solo) and went to go make an index page. I have been attempting to follow django best practices as I go, so naturally I go search for this but couldn't a real standard in regards to this.
I have seen folks creating apps to serve this purpose named various things (main, home, misc) and have seen a views.py in the root of the project. I am really just looking for what the majority out there do for this.
The index page is not static, since I want to detect if the user is logged in and such.
Thanks.
If all of your dynamic content is handled in the template (for example, if it's just simple checking if a user is present on the request), then I recommend using a generic view, specificially the direct to template view:
urlpatterns = patterns('django.views.generic.simple',
(r'^$', 'direct_to_template', {'template': 'index.html'}),
)
If you want to add a few more bits of information to the template context, there is another argument, extra_context, that you can pass to the generic view to include it:
extra_context = {
'foo': 'bar',
# etc
}
urlpatterns = patterns('django.views.generic.simple',
(r'^$', 'direct_to_template', {'template': 'index.html', 'extra_context': extra_context }),
)
I tend to create a views.py in the root of the project where I keep the index view.