django-views

How to create link for one of the category in Django

谁说胖子不能爱 提交于 2019-12-02 11:47:15
I'm coding a news website.I have 'category' in News model. I want to get all the news in one of the categories named 'opinion' in my index.html. And create detail page link for each of them. I can the title ,author, etc of the news mentioned above .But my brain really junks,I don't know how to create a link points to opinion_new.html or news_detail.htlm for each of them.I have a link for regular news to point to news_detail.htlm. If you don't quite understand what I'm asking, please also read my last question How to get all the post in the same category in Django you so much! here is part of

How to pass a product download link inside the django template

偶尔善良 提交于 2019-12-02 11:10:28
how do i pass the link to download a product if the particular product is downloadable models.py protected_loc = settings.PROTECTED_UPLOADS def download_loc(instance,filename): return "%s/%s" %(instance.slug, filename) # if instance.user.username: # return "%s/download/%s" %(instance.user.username,filename) # else: # return "%s/download/%s" %("default",filename) class Product(models.Model): user = models.ForeignKey(settings.AUTH_USER_MODEL,on_delete=models.CASCADE,null=True,blank=True) name = models.CharField(max_length=120) description = models.CharField(max_length=500,null=True,blank=True)

How to write a single view function to render multiple html from diferent urls path?

陌路散爱 提交于 2019-12-02 10:50:17
问题 I am trying to avoid self coping of views functions but have no idea ho to do it. My views have a minor differences and there is definitely a way to render html pages with a single function in this case. I experimented with urls "name" value but failed. I just started with django and hope that experienced programers have a solution. Thank you for your help. urls.py urlpatterns = [ path('admin/', admin.site.urls), path('', views.index, name='index'), path('about/', views.about, name='about'),

Intermittent 403 response from django.contrib.auth.views.login()

拟墨画扇 提交于 2019-12-02 10:43:42
Using django.contrib.auth.views.login() to process user logins I'm seeing 403 responses in a production environment. A second attempt to login succeeds after an initial 403 (when that response occurs). I've begun to log all 403 login failures, capturing the POST payload and cookie values which shows that csrfmiddlewaretoken (the hidden form field value) and csrftoken (cookie value) don't match. It's intermittent and happens to many users. The following decorators are all applied to the login function being used to proxy the django.contrib.auth.views.login() function: @ensure_csrf_cookie,

Django Ajax Form submit wrongly redirect to another page

霸气de小男生 提交于 2019-12-02 10:34:52
When I use ajax to submit a comment form in Django,the page will redirect to a blank page shows me the success data: {"status":"success", "msg":"添加成功"} ,but not stay in current page.I want the page stay in current page and show me the new comment. Here is my update_comment view: def update_comment(request, news_pk): news = get_object_or_404(News, id=news_pk) comment_form = CommentForm(request.POST or None) if request.method == 'POST' and comment_form.is_valid(): if not request.user.is_authenticated: return render(request, 'login.html', {}) comments = comment_form.cleaned_data.get("comment")

Ajax not work in Django post

你离开我真会死。 提交于 2019-12-02 10:31:32
I'm trying to use ajax in Django to post comment in a news website.However it doesn't work. When I click the submit button,it still refreshes the page and make no difference like no ajax. I'm really new in Django and Ajax.Any friend can help me solve it? Here is my view.py: def newsDetailView(request, news_pk): news = News.objects.get(id=news_pk) title = news.title author = news.author_name add_time = news.add_time content = news.content category = news.category tags = news.tag.annotate(news_count=Count('news')) all_comments = NewsComments.objects.filter(news=news) comment_form = CommentForm

form wizard initial data for edit not loading properly in Django?

牧云@^-^@ 提交于 2019-12-02 10:19:43
I have a three page form-list coming out of a single model. I could save the model first time, but when I want to edit the model, only the first form shows the initial value, subsequent forms does not show the initial data. but when I print the initial_dict from views, I can see all the initial views correctly. I followed this blog on form wizard. Here is my model.py: class Item(models.Model): user=models.ForeignKey(User) price=models.DecimalField(max_digits=8,decimal_places=2) image=models.ImageField(upload_to="assets/", blank=True) description=models.TextField(blank=True) def __unicode__

Limit values in the modelformset field

为君一笑 提交于 2019-12-02 10:17:38
I've been trying to solve this problem for a couple of days now, getting quite desperate. See the commented out code snippets for some of the things I've tried but didn't work. Problem: How can I limit the values in the category field of the IngredientForm to only those belonging to the currently logged in user? views.py @login_required def apphome(request): IngrFormSet = modelformset_factory(Ingredient, extra=1, fields=('name', 'category')) # Attempt #1 (not working; error: 'IngredientFormFormSet' object has no attribute 'fields') # ingrformset = IngrFormSet(prefix='ingr', queryset=Ingredient

Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/

旧巷老猫 提交于 2019-12-02 10:04:26
I was following the django documentation and making a simple poll app. I have come across the following error : Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order: ^polls/ ^admin/ The current URL, , didn't match any of these." settings.py ROOT_URLCONF = 'mysite.urls' mysite/mysite/urls.py from django.conf.urls import include,url from django.contrib import admin urlpatterns = [ url(r'^polls/',include('polls.urls')), url(r'^admin/', admin.site.urls),] mysite/polls/urls.py from django.conf.urls import url from . import views app_name= 'polls' urlpatterns=[

badly formed hexadecimal UUID error string for a UUID primary key

不问归期 提交于 2019-12-02 10:01:13
I'm trying to get my table to load, but am getting this error, most likely because my primary key is a uuid. Here is my url paths. path('results/', views.results, name='results'), path('results/<uuid:pk>/', views.genre_book, name='books_genre_table'), Here is my views.py If there is one observation, we load the price table. If not, then we load a table with service descriptions. The user can then pick the service they are most interested and click through to the price table for that service. This is not working. The first view "results" works, but the second view book_genre does not. def