django-views

global name 'request' is not defined: overriding form_valid

夙愿已清 提交于 2019-12-12 03:16:33
问题 I have an UpdateView which I am overriding the form_valid method. For some reason 'request' is shown as being not defined within the overridden method. Here is the full class: class UpdateTopic(UpdateView): model = Post slug_field = 'pk' slug_url_kwarg = 'pk' form_class = CommentForm template_name = "forums/update_topic.html" def form_valid(self, form): user = self.request.user rep = self.request.user.player.get_rep_total # protect the system against url input attacks if user == self.object

Issues with Views and forms

五迷三道 提交于 2019-12-12 02:43:53
问题 I have given the url in my urls.py file as url(r'^register/$', 'drinker.views.drinker_reg'), and I have created the drinker_reg view in my views.py file. But still I am getting the error ViewDoesNotExist at /register Could not import drinker.views.drinker_reg. View does not exist in module drinker.views. I notice one thing that when I used drinker.forms import RegistrationForm only then I am getting the error otherwise it read the drinker_reg view The code of views.py is : from django.http

Type error with django class based view

旧城冷巷雨未停 提交于 2019-12-12 02:37:43
问题 I get this error TypeError at /author/list/4 super(type, obj): obj must be an instance or subtype of type Exception Location: /home/ronald/best/A2/0124/vort/larb/views.py in get_context_data, line 140 context = super(AuthorCreate, self).get_context_data(**kwargs) url.py url(r'^author/list/(?P<user_id>\d+)$', AuthorList.as_view(), name='author_list' ), views.py for listview class AuthorList(LoginRequiredMixin, ListView): template_name = 'authorList.html' queryset = Author.objects.all() def get

UserCreationForm show error when fields are empty

こ雲淡風輕ζ 提交于 2019-12-12 02:15:38
问题 I'm using Django built in UserCreationForm. I want to show message under the field when that field is empty and user tring to submit form. Unfortunatly I see only built-in behavior of browsers like "fill out this field", by the way in different browsers that behavior is different. Some browsers just encircle the field box with a red line. How to turn off this behavior and show message under the field. Why .error_messages didnt work?! Also I use {{ form.field_name.errors }} in my template.

Ajax GET data returning 'None'

て烟熏妆下的殇ゞ 提交于 2019-12-12 01:47:49
问题 Here's my ajax function: $('a.username').on('click', function() { var username = $(this).html(); var url = window.location.href.split('?')[0]; $.ajax({ type: 'GET', url: url, data: { username_clicked: username, csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val() }, success: function (data) { console.log(data.username_clicked) } }) }); And template: <h3><a href="{% url 'raise_profile' %}" class="username">{{ i.author }}</a></h3> url url(r'^raise_profile/', raise_profile, name=

Combine two models with OneToOne relationship into one form - Django

随声附和 提交于 2019-12-12 01:46:00
问题 I created two custom user models (AbstractBaseUser and a separate for extra information). Is there a way to combine the two models to create one form that the user can use to update all of their information between the two models? For example, it would be ideal to have something like this (although I know not possible): class ProfileChangeForm(forms.ModelForm): class Meta: model = UserProfile fields = ['username', 'email', first_name', 'last_name', 'bio', 'website'] Thank you in advance for

How to get /test/?grid-view or /test/?list-view' in Django view

落爺英雄遲暮 提交于 2019-12-12 01:42:32
问题 I have a page that can be viewed by list or grid view through jQuery, but I want the URL to display what view it is -- especially when user paginate so that they don't have to reclick what they want to view -- so something like this: /test/?grid_view or /test/?list_view . I've been trying to use request.GET.get , but it doesn't seem to be working. Here is what I have so far: def test (request): grid_view = request.GET.get('grid_view') list_view = request.GET.get('list_view') if grid_view:

How to configure Django views and urls to render specific templates

瘦欲@ 提交于 2019-12-12 01:27:47
问题 When I bring up 127.0.0.1:8000, the current page that show up is something.html template. I would need to make it appear index.html at first launch, then when I click on other parts,it should go to 127.0.0.1:8000/something.html (or 127.0.0.1:8000/myapp/something.html). What would be the structure to achieve this? I frequently get error message : The current URL didn't match any of these. Currently, my structure is project ---myapp ---admin.py ---models.py ---url.py ---views.py ---static --

'NoneType' object has no attribute 'user'

大城市里の小女人 提交于 2019-12-12 01:03:07
问题 models.py class Report(models.Model): user = models.ForeignKey(User, null=False) photo_files_attached = models.BooleanField('Photos', default=False) views.py def media(request): user = request.user try: report = Report.objects.get(user=user.id) except: report = None report_dir = str(report.user.id) + '/' + str(report.id) output_dir = settings.MEDIA_ROOT + '/' + report_dir imagelist = [] if os.path.exists(output_dir): os.chdir(output_dir) for files in os.listdir("."): for extension in JPEG

User selects language on login page using form django

↘锁芯ラ 提交于 2019-12-12 00:47:50
问题 I have a login page with the following form: <form class="login-form" name="LoginForm" action="{% url 'login' %}" method="post"> {% csrf_token %} <p> <table> <tr> <td>{{ form.username.label_tag }} </td> <td>{{ form.username }}</td> </tr> <tr> <td>{{ form.password.label_tag }}</td> <td>{{ form.password }}</td> </tr> </table> </p> <input class="button button-small" type="submit" value="Login" /> <input type="hidden" name="next" value="{{ next }}" /> </form> <a>English</a> <a>French</a> and I